• Python_结合Re正则模块爬虫


    ##### 爬取古诗文
    import re
    import requests

    def parse_page(url):
    headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36'
    }
    respose = requests.get(url,headers).text
    titles = re.findall(r'<divsclass="cont">.*?<b>(.*?)</b>',respose,re.DOTALL)
    authors = re.findall(r'<psclass="source">.*?<as.*?>(.*?)</a>',respose,re.DOTALL)
    dynaties = re.findall(r'<psclass="source">.*?<as.*?><as.*?>(.*?)</a>',respose,re.DOTALL)
    content_tags = re.findall(r'divsclass="contson"sid=.*?>(.*?)</div>',respose,re.DOTALL)
    contents = []
    for content in content_tags:
    # 替换<b>
    text = re.sub(r'<.*?>','',content)
    contents.append(text.strip())

    poems = []
    # zip进行解包
    for values in zip(titles,authors,dynaties,contents):
    titles,authors,dynaties,content = values
    poem = {
    'titles':titles,
    'authors':authors,
    'dynaties':dynaties,
    'content_tags':content
    }
    poems.append(poem)

    for poem in poems:
    print(poem)
    print('*'*40)

    def main():
    for x in range(1,10):
    url = 'https://www.gushiwen.cn/default_%d.aspx'%x
    parse_page(url)

    if __name__ == '__main__':
    main()



    # 正则爬取糗事百科段子

    import re
    import requests

    def parse_page(url):
    headers = {
    'user-agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36'
    }
    respone = requests.get(url,headers=headers).text
    # re.DOTALL = re.S 可以.匹配
    contents = re.findall(r'<divsclass="content">.*?<span>(.*?)</span>',respone,re.DOTALL)
    content_text = []
    for content in contents:
    text = re.sub(r'<.*?>',"",content)
    content_text.append(text.strip())

    DUNA_Text = []
    for value in zip(content_text):
    contents = value
    text = {
    "title":contents
    }
    DUNA_Text.append(text)

    for duan_value in DUNA_Text:
    print(duan_value)

    def main():

    for x in range(1,10):
    url = 'https://www.qiushibaike.com/text/page/%d/'%x
    parse_page(url)

    if __name__ == '__main__':
    main()
  • 相关阅读:
    WebApi 接口参数不再困惑:传参详解
    dataType和contentType的区别
    WaitHandles 的数目必须少于或等于 64 个--任意线程信号量监视
    跨线程调用DataGridView控件
    (转)调整.NET控件WebBrowser的默认浏览器内核版本
    Winform中checklistbox控件的常用方法
    csuoj 残缺的棋盘
    csuoj 你经历过绝望吗?两次! bfs + 优先队列
    csuoj barricade
    csuoj 集训队分组
  • 原文地址:https://www.cnblogs.com/Teachertao/p/14191209.html
Copyright © 2020-2023  润新知