python正则爬取微信阅读总榜单写入csv

python正则爬取微信阅读总榜单写入csv,第1张

python正则爬取微信阅读总榜单写入csv
# 爬取微信阅读top总榜
import requests
import re
import csv
url = "https://weread.qq.com/web/category/all"
res = requests.get(url)
res.encoding = 'utf-8'
# print(res.text)

obj = re.compile(r'

(?Pd+)

' r'.*?

(?P.*?)

' r'

<.*?>(?P.*?).*?

(' r'?P.*?)

', re.S) # obj = re.compile(r'

(?P.*?)

', re.S) result = obj.finditer(res.text) books = [] for item in result: dic = item.groupdict() dic['作者'] = dic.pop('author') dic['排名'] = dic.pop('index') dic['书名'] = dic.pop('name') dic['描述'] = dic.pop('description') books.append(dic) print(dic) header = ["排名", "作者", "书名", "描述"] # newline是数据之间不加空行 with open('weixingRead.csv', 'w', newline='', encoding='utf-8') as f: writer = csv.DictWriter(f, fieldnames=header) # 提前预览列名 writer.writeheader() writer.writerows(books)

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5679984.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存