本文的文字及圖片過濾網絡,能夠學習,交流使用,不具備任何商業用途,若有問題請及時聯繫咱們以做處理。css
Python爬蟲、數據分析、網站開發等案例教程視頻免費在線觀看html
https://space.bilibili.com/523606542
1,爬取青燈編程公衆號全部的文章python
2,爬取全部關於python的公衆號文章web
1,登錄公衆號以後點擊圖文ajax
2,打開開發者工具編程
3,點擊超連接json
相關的數據加載出來,就有關於數據包,包含了文章標題,連接,摘要,發佈時間等等,也能夠選擇其餘的公衆號也能夠爬取的,可是這須要你有一個微信公衆號。微信
要加cookiecookie
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)
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)