python抓取搜狗微信公眾號(hào)文章
初學(xué)python,抓取搜狗微信公眾號(hào)文章存入mysql
mysql表:


代碼:
import requests
import json
import re
import pymysql
# 創(chuàng)建連接
conn = pymysql.connect(host='你的數(shù)據(jù)庫(kù)地址', port=端口, user='用戶名', passwd='密碼', db='數(shù)據(jù)庫(kù)名稱(chēng)', charset='utf8')
# 創(chuàng)建游標(biāo)
cursor = conn.cursor()
cursor.execute("select * from hd_gzh")
effect_row = cursor.fetchall()
from bs4 import BeautifulSoup
socket.setdefaulttimeout(60)
count = 1
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0'}
#阿布云ip代理暫時(shí)不用
# proxyHost = "http-cla.abuyun.com"
# proxyPort = "9030"
# # 代理隧道驗(yàn)證信息
# proxyUser = "H56761606429T7UC"
# proxyPass = "9168EB00C4167176"
# proxyMeta = "http://%(user)s:%(pass)s@%(host)s:%(port)s" % {
# "host" : proxyHost,
# "port" : proxyPort,
# "user" : proxyUser,
# "pass" : proxyPass,
# }
# proxies = {
# "http" : proxyMeta,
# "https" : proxyMeta,
# }
#查看是否已存在數(shù)據(jù)
def checkData(name):
sql = "select * from gzh_article where title = '%s'"
data = (name,)
count = cursor.execute(sql % data)
conn.commit()
if(count!=0):
return False
else:
return True
#插入數(shù)據(jù)
def insertData(title,picture,author,content):
sql = "insert into gzh_article (title,picture,author,content) values ('%s', '%s','%s', '%s')"
data = (title,picture,author,content)
cursor.execute(sql % data)
conn.commit()
print("插入一條數(shù)據(jù)")
return
for row in effect_row:
newsurl = 'https://weixin.sogou.com/weixin?type=1&s_from=input&query=' + row[1] + '&ie=utf8&_sug_=n&_sug_type_='
res = requests.get(newsurl,headers=headers)
res.encoding = 'utf-8'
soup = BeautifulSoup(res.text,'html.parser')
url = 'https://weixin.sogou.com' + soup.select('.tit a')[0]['href']
res2 = requests.get(url,headers=headers)
res2.encoding = 'utf-8'
soup2 = BeautifulSoup(res2.text,'html.parser')
pattern = re.compile(r"url \+= '(.*?)';", re.MULTILINE | re.DOTALL)
script = soup2.find("script")
url2 = pattern.search(script.text).group(1)
res3 = requests.get(url2,headers=headers)
res3.encoding = 'utf-8'
soup3 = BeautifulSoup(res3.text,'html.parser')
print()
pattern2 = re.compile(r"var msgList = (.*?);$", re.MULTILINE | re.DOTALL)
script2 = soup3.find("script", text=pattern2)
s2 = json.loads(pattern2.search(script2.text).group(1))
#等待10s
time.sleep(10)
for news in s2["list"]:
articleurl = "https://mp.weixin.qq.com"+news["app_msg_ext_info"]["content_url"]
articleurl = articleurl.replace('&','&')
res4 = requests.get(articleurl,headers=headers)
res4.encoding = 'utf-8'
soup4 = BeautifulSoup(res4.text,'html.parser')
if(checkData(news["app_msg_ext_info"]["title"])):
insertData(news["app_msg_ext_info"]["title"],news["app_msg_ext_info"]["cover"],news["app_msg_ext_info"]["author"],pymysql.escape_string(str(soup4)))
count += 1
#等待5s
time.sleep(10)
for news2 in news["app_msg_ext_info"]["multi_app_msg_item_list"]:
articleurl2 = "https://mp.weixin.qq.com"+news2["content_url"]
articleurl2 = articleurl2.replace('&','&')
res5 = requests.get(articleurl2,headers=headers)
res5.encoding = 'utf-8'
soup5 = BeautifulSoup(res5.text,'html.parser')
if(checkData(news2["title"])):
insertData(news2["title"],news2["cover"],news2["author"],pymysql.escape_string(str(soup5)))
count += 1
#等待10s
time.sleep(10)
cursor.close()
conn.close()
print("操作完成")
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決tensorflow讀取本地MNITS_data失敗的原因
這篇文章主要介紹了解決tensorflow讀取本地MNITS_data失敗的原因,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06
python一行代碼就能實(shí)現(xiàn)數(shù)據(jù)分析的pandas-profiling庫(kù)
這篇文章主要為大家介紹了python一行代碼就能實(shí)現(xiàn)數(shù)據(jù)分析的pandas-profiling庫(kù),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
Python中print和return的作用及區(qū)別解析
print的作用是輸出數(shù)據(jù)到控制端,就是打印在你能看到的界面上。這篇文章給大家介紹Python中print和return的作用及區(qū)別解析,感興趣的朋友跟隨小編一起看看吧2019-05-05
python opencv 畫(huà)外接矩形框的完整代碼
這篇文章主要介紹了python-opencv-畫(huà)外接矩形框的實(shí)例代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
Python實(shí)現(xiàn)五子棋聯(lián)機(jī)對(duì)戰(zhàn)小游戲
本文主要介紹了通過(guò)Python實(shí)現(xiàn)簡(jiǎn)單的支持聯(lián)機(jī)對(duì)戰(zhàn)的游戲——支持局域網(wǎng)聯(lián)機(jī)對(duì)戰(zhàn)的五子棋小游戲。廢話不多說(shuō),快來(lái)跟隨小編一起學(xué)習(xí)吧2021-12-12
python利用tkinter實(shí)現(xiàn)屏保
這篇文章主要為大家詳細(xì)介紹了python利用tkinter實(shí)現(xiàn)屏保,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
python三種數(shù)據(jù)標(biāo)準(zhǔn)化方式
這篇文章主要介紹了python三種數(shù)據(jù)標(biāo)準(zhǔn)化方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
tensorflow模型文件(ckpt)轉(zhuǎn)pb文件的方法(不知道輸出節(jié)點(diǎn)名)
這篇文章主要介紹了tensorflow模型文件(ckpt)轉(zhuǎn)pb文件(不知道輸出節(jié)點(diǎn)名),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04

