栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

爬虫使用正则表达式爬取古文网

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

爬虫使用正则表达式爬取古文网

import requests
import re
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62',
}
def par_html(url):
    resp=requests.get(url,headers=headers)
    text=resp.text
    title=re.findall(r'.*?.*?(.*?)',text)
    zuozhe=re.findall(r'.*?.*?(.*?)',text)
    contents=re.findall(r'.*?(.*?)',text,re.S)
    poems=[]
    for i in zip(title,zuozhe,contents):
        title,zuozhe,contents=i
        poem={
            "title":title,
            "zuozhe":zuozhe,
            "contents":contents
        }
        poems.append(poem)
    # print(poems)
    for i in poems:

        print(i["title"])
        print(i["zuozhe"])
        print(i["contents"])
        print("*"*30)

def main():
    url="https://www.gushiwen.com/type/n/lianghan/n/2.html"
    par_html(url)

main()

爬取的结果

 爬虫小案例实例总结

链接:https://pan.baidu.com/s/1V19M39M094hXeuqUSzPMnw?pwd=xnfn
提取码:xnfn
--来自百度网盘超级会员V2的分享

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/700823.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号