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

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/')
相关推荐
SEO_juper15 小时前
用 Python 写一个 GEO 可见性检查脚本:你的网站现在能被 AI 引用吗
开发语言·人工智能·爬虫·python·seo·外贸独立站
阿狗童鞋1 天前
Python爬虫进阶实战指南
开发语言·爬虫·python
深蓝电商API1 天前
protobuf逆向:从抓包乱码到还原数据结构
爬虫·protobuf
小静AI工程实验室2 天前
Python 爬虫解析 JSON-LD:多块 script、@graph 与坏数据的 9 个边界
爬虫·python·json
要吃这碗饭2 天前
YouTube 数据抓取出现 429/403 错误?2026 反爬机制拆解与爬虫选型指南
网络·爬虫·网络协议
袁袁袁袁满2 天前
AI Agent 如何“看懂“互联网?
爬虫·python·自动化·爬虫实战·多线程爬虫
Helix2502 天前
Python爬虫零基础实战:3步抓取网页数据并导出Excel
爬虫·python·beautifulsoup·excel·python教程·requests·网页数据
深蓝电商API3 天前
Frida入门到实战:hook so层与Java层的姿势总结
爬虫·frida
小静AI工程实验室3 天前
robots.txt 不是门禁:RFC 9309 规则、缓存与 Python 爬虫的 10 个边界
爬虫·python·缓存
远航计算机3 天前
AI 爬虫分三种,你 robots.txt 里挡的是哪一种?
人工智能·爬虫·aigc