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

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/')
相关推荐
小静AI工程实验室4 小时前
Python 爬虫中文乱码排查:严格解码、UTF-8 BOM 与 JSON 转义的 12 项实验
字符编码·爬虫·python
咖啡星人k11 小时前
从自建爬虫到托管工具:数据采集选型对照表
爬虫·数据采集·mcp
电商API_180079052471 天前
拍照找同款是怎么实现的?淘宝以图搜图接口 item_search_img 对接实战
大数据·爬虫·数据挖掘·数据分析·代采api
Patrick在香港1 天前
Python 抓 0.91 GB 香港法例:Agent 的进度该写进磁盘,不是写进上下文
爬虫·python·api·claude·香港
Experience-摆渡1 天前
开源RAG知识库WeKnora深度调研:让知识自己长成体系
爬虫·docker·开源·rag
孙启超2 天前
【AI开发之Rust】第 7 课:错误处理 —— panic、Result 与 `?`
人工智能·分布式·后端·爬虫·spring cloud·架构·rust
该逃避避3 天前
IP代理类型介绍:住宅IP、机房IP、移动IP等区别与选择
爬虫
科技苑3 天前
Python简单网络爬虫教程
爬虫·python
szephyr3 天前
Python 爬虫合规与反爬实战:从 requests 到 Playwright
爬虫·python·requests·playwright·反爬
深蓝电商API3 天前
MCP 与 AI Agent 如何改变爬虫开发模式?
爬虫·agent·mcp