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

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/')
相关推荐
wuyk5551 天前
Spider-第16讲-线程池与竞态问题
大数据·爬虫·python·elasticsearch·搜索引擎
wuyk5553 天前
Spider-第14讲-表单提交与CSRF
前端·爬虫·python·elasticsearch·csrf
wuyk5555 天前
Spider-第11讲-工程化改造与可维护性
大数据·爬虫·elasticsearch·搜索引擎
wuyk5555 天前
Spider-第12讲-asyncio异步并发爬取
大数据·爬虫·elasticsearch·搜索引擎
小宋10215 天前
AI 爬虫把大学图书馆站点拖垮:按行为指纹给爬虫分类、按类发配额、用边缘缓存分层,以带宽成本结构和真实用户 P95 延迟定档
人工智能·爬虫·分类
wuyk5556 天前
Spider-第10讲-SQLite持久化与去重
数据库·爬虫·sqlite
wuyk5556 天前
Spider-第09讲-三格式落盘CSV-Excel-JSON
爬虫·python·json·excel
wuyk5557 天前
Spider-第07讲-JSON接口与正则提取
爬虫·python·json
feasibility.7 天前
一物降一物?AI逆向没有终局:REA拆App,BLA设套,LASIK清场
人工智能·爬虫·github·逆向·skill·rea
wuyk5559 天前
Spider-第03讲-HTTP速通与读懂响应
网络·爬虫·网络协议·http