正则匹配与爬虫爬取图片路径综合练习

1.使用正则完成下列内容的匹配

  • 匹配陕西省区号 029-12345

  • 匹配邮政编码 745100

  • 匹配邮箱 lijian@xianoupeng.com

  • 匹配身份证号 62282519960504337X

python 复制代码
import re
print(bool(re.fullmatch(r'^029-\d+$', '029-12345')))
print(bool(re.fullmatch(r'^\d{6}$', '745100')))
print(bool(re.fullmatch(r'^\w+@\w+\.\w+$', 'lijian@xianoupeng.com')))
print(bool(re.fullmatch(r'^\d{17}[\dXx]$', '62282519960504337X')))

2.爬取学校官网,获取所有图片途径并将路径存储在本地文件中,使用装饰器完成

python 复制代码
import requests
from bs4 import BeautifulSoup
import time

def log(f):
    def w(url):
        urls = f(url)
        print(f'[{time.strftime("%Y-%m-%d %H:%M:%S")}] 共爬取{len(urls)}张图片')
        return urls
    return w

@log
def get_imgs(url):
    try:
        res = requests.get(url, headers={'User-Agent':'Mozilla/5.0'}, timeout=10)
        soup = BeautifulSoup(res.text, 'html.parser')
        imgs = [i.get('src') for i in soup.find_all('img') if i.get('src')]
        with open('img_urls.txt', 'w', encoding='utf-8') as f:
            f.write('\n'.join(imgs))
        return imgs
    except:
        print('爬取失败')
        return []

get_imgs('https://nhjcxy.edu.cn/')
相关推荐
嫂子的姐夫24 分钟前
039-DES:gov招标(解密)
爬虫·python·js逆向·逆向
axinawang29 分钟前
httpbin
爬虫
Cory.眼1 小时前
ZLibrary反爬策略全解析
爬虫·反爬策略·zlibrary
05大叔2 小时前
网络机器人(爬虫)-入门
爬虫
xifangge20252 小时前
Python 爬虫实战:爬取豆瓣电影 Top250 数据并进行可视化分析
开发语言·爬虫·python
海边的梦21 小时前
【无标题】
爬虫·网络爬虫
@我漫长的孤独流浪1 天前
Python爬虫实战:从入门到精通
开发语言·爬虫·python
小白学大数据1 天前
如何判断网站流量飙升是搜索引擎爬虫导致的?
爬虫·scrapy·搜索引擎·pycharm
devnullcoffee1 天前
亚马逊ASIN数据批量采集技术选型:工具 vs 自建爬虫 vs Scrape API vs AI Agent完整对比
人工智能·爬虫·agent·亚马逊运营·openclaw·亚马逊 asin 数据采集
MuShan-bit1 天前
CSDN-推荐开源项目-auto-x-to-wechat
爬虫·微信·开源·node.js·twitter