querylist采集微信公众号文章(爬取所有关于python的公众号文章,你了解多少? )

优采云 发布时间: 2021-12-31 17:22

  querylist采集微信公众号文章(爬取所有关于python的公众号文章,你了解多少?

)

  前言

  本文中的文字图片过滤网络可用于学习、交流,不具有任何商业用途。如果您有任何问题,请联系我们进行处理。

  文章为基础开发环境爬取两个公众号:

  1.抓取所有 文章

  2、爬取所有关于python的公众号文章

  爬取 文章

  1、登录公众号后点击图片和文字

  

  2、打开开发者工具

  

  3、点击超链接

  

  加载相关数据时,有一个数据包,包括文章标题、链接、摘要、发布时间等,你也可以选择其他公众号进行爬取,但这需要你有一个微信公众号。

  添加cookie

  import pprint

import time

import requests

import csv

f = open('青灯公众号文章.csv', mode='a', encoding='utf-8', newline='')

csv_writer = csv.DictWriter(f, fieldnames=['标题', '文章发布时间', '文章地址'])

csv_writer.writeheader()

for page in range(0, 40, 5):

url = f'https://mp.weixin.qq.com/cgi-bin/appmsg?action=list_ex&begin={page}&count=5&fakeid=&type=9&query=&token=1252678642&lang=zh_CN&f=json&ajax=1'

headers = {

'cookie': '加cookie',

'referer': 'https://mp.weixin.qq.com/cgi-bin/appmsg?t=media/appmsg_edit_v2&action=edit&isNew=1&type=10&createType=0&token=1252678642&lang=zh_CN',

'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36',

}

response = requests.get(url=url, headers=headers)

html_data = response.json()

pprint.pprint(response.json())

lis = html_data['app_msg_list']

for li in lis:

title = li['title']

link_url = li['link']

update_time = li['update_time']

timeArray = time.localtime(int(update_time))

otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)

dit = {

'标题': title,

'文章发布时间': otherStyleTime,

'文章地址': link_url,

}

csv_writer.writerow(dit)

print(dit)

  爬取所有关于python的公众号文章

  1、搜狗搜索python选择微信

  

  注意:如果不登录,只能抓取前十页数据。登录后可以抓取2W多篇文章文章。

  2.直接爬取静态网页的标题、公众号、文章地址、发帖时间。

  import time

import requests

import parsel

import csv

f = open('公众号文章.csv', mode='a', encoding='utf-8', newline='')

csv_writer = csv.DictWriter(f, fieldnames=['标题', '公众号', '文章发布时间', '文章地址'])

csv_writer.writeheader()

for page in range(1, 2447):

url = f'https://weixin.sogou.com/weixin?query=python&_sug_type_=&s_from=input&_sug_=n&type=2&page={page}&ie=utf8'

headers = {

'Cookie': '自己的cookie',

'Host': 'weixin.sogou.com',

'Referer': 'https://www.sogou.com/web?query=python&_asf=www.sogou.com&_ast=&w=01019900&p=40040100&ie=utf8&from=index-nologin&s_from=index&sut=1396&sst0=1610779538290&lkt=0%2C0%2C0&sugsuv=1590216228113568&sugtime=1610779538290',

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36',

}

response = requests.get(url=url, headers=headers)

selector = parsel.Selector(response.text)

lis = selector.css('.news-list li')

for li in lis:

title_list = li.css('.txt-box h3 a::text').getall()

num = len(title_list)

if num == 1:

title_str = 'python' + title_list[0]

else:

title_str = 'python'.join(title_list)

href = li.css('.txt-box h3 a::attr(href)').get()

article_url = 'https://weixin.sogou.com' + href

name = li.css('.s-p a::text').get()

date = li.css('.s-p::attr(t)').get()

timeArray = time.localtime(int(date))

otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)

dit = {

'标题': title_str,

'公众号': name,

'文章发布时间': otherStyleTime,

'文章地址': article_url,

}

csv_writer.writerow(dit)

print(title_str, name, otherStyleTime, article_url)

  

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线