日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python 公众号爬虫_python_爬虫_微信公众号抓取

發(fā)布時(shí)間:2023/12/15 python 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 公众号爬虫_python_爬虫_微信公众号抓取 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

importrequests,pymysqlimportjson,jsonpath,random,re,time,datetime,os,imghdrfrom lxml importetreefrom selenium importwebdriverfrom urllib importrequestimportssl

ssl._create_default_https_context=ssl._create_unverified_context'''注意點(diǎn):如果同一時(shí)間內(nèi)刷新次數(shù),或者獲取分頁(yè)太頻繁,會(huì)被封'''

#--------------------

user_info = {'username':'####@163.com','password':'####'}

base_url= 'https://mp.weixin.qq.com/'base_headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'}

query_list= [ #需要抓取的公眾號(hào)列表

{'fakeid':'MzIzOTQ0MTUwMA==','nickname':'Sir電影'},

{'fakeid':'MzIxODc5MzM4NQ==','nickname':'魚Sir電影'},

]

table= 'p_weixin' #數(shù)據(jù)庫(kù)名稱

key = 'title,author,js_name,publish_time,images,vedios'

#--------------------

def get_cookie(): #登陸并獲取cookie值

driver = webdriver.Chrome(executable_path=r'/Applications/Google Chrome.app/chromedriver')

driver.get(base_url)

time.sleep(2) #頁(yè)面緩沖

driver.find_element_by_name('account').clear()

driver.find_element_by_name('account').send_keys(user_info['username'])

driver.find_element_by_name('password').clear()

driver.find_element_by_name('password').send_keys(user_info['password'])

driver.find_element_by_class_name('icon_checkbox').click()

driver.find_element_by_class_name('btn_login').click()

time.sleep(25) #等待手機(jī)掃碼

c_total=driver.get_cookies()

cookies= {} #cookies存儲(chǔ)

for i inc_total:

cookies[i['name']] = i['value']returncookiesdef get_info(): #獲取公眾號(hào)名稱、總頁(yè)數(shù)、token、fakeid等信息,

cookies =get_cookie()

res_token= requests.get(base_url,cookies=cookies,headers=base_headers)

token= re.compile(r'token=(\d+)').findall(str(res_token.url))for query in query_list: #從列表里控制要爬取多少個(gè)公眾號(hào)

url = 'https://mp.weixin.qq.com/cgi-bin/appmsg' #公眾號(hào)里面的電影

fakeid= query['fakeid']

appmsg={'token': token,'lang': 'zh_CN','f': 'json','ajax': '1','random': random.random(),'action': 'list_ex','begin': '0','count': '5','fakeid': fakeid,'type': '9',

}

res_cnt= requests.get(url,params=appmsg,cookies=cookies) #看總共多少頁(yè) 一頁(yè)16篇,begin 0~4為一頁(yè)

res_cnt =json.loads(res_cnt.text)for cnt in range(0,res_cnt['app_msg_cnt'],5): #循環(huán)所有分頁(yè)

appmsg['begin'] = cnt #當(dāng)前的分頁(yè)值

response = requests.get(url,params=appmsg,cookies=cookies)

data_list=json.loads(response.text)for data in data_list['app_msg_list']: #對(duì)當(dāng)前頁(yè)里的信息進(jìn)行提取

yield [data['title'],data['link']]

time.sleep(random.randrange(30,41)) #設(shè)置間隔

print('公眾號(hào):%s,共有文章%s' % (query['nickname'],res_cnt['app_msg_cnt'])) #返回頁(yè)數(shù),這里應(yīng)該單獨(dú)輸入,不需要存儲(chǔ)

defget_news():#def get_news(url): # 獲取文章,這里可以用協(xié)程?也需要換agent

'''視頻、音頻、圖片'''

print('-'*40)

url= 'https://mp.weixin.qq.com/s?src=3&timestamp=1533397256&ver=1&signature=RbnX4tUBODpql9qsvp4jJRDrtHc-LSXXm9gSM*BNY*PTRKHJ2bUyeKkGPlpKGGsnKl4IyaxubTPPWv6jQzhm52M7qFY5*BJ8dEugb4XPUcLRSs8U-4Bb9ab9mso2NWDq0*RwRzZ2*zZ6r1YyQtNjpg=='res= request.Request(url,headers=base_headers)

response=request.urlopen(res)

re_data=response.read().decode()

data=etree.HTML(re_data)

title= get_try(data, '//h2[@id="activity-name"]/text()') #標(biāo)題

author = get_try(data, '//div[@id="meta_content"]//span[@class="rich_media_meta rich_media_meta_text"]//text()') #作者

js_name = get_try(data, '//div[@id="meta_content"]//span[@class="rich_media_meta rich_media_meta_text"]//text()') #公眾號(hào)名稱

publish_time = re.compile(r'var publish_time.*?\"(.*?)\"').findall(re_data)[0] #發(fā)布時(shí)間 昨天、前天、今天、1周前

images_list= [] #圖片

vedio_list = [] #音視頻

#還有圖片、視頻、音頻地址

js_content = data.xpath('//div[@id="js_content"]//p//text()|//div[@id="js_content"]//p//img/@data-src|//div[@id="js_content"]//p//iframe/@data-src|//mpvoice')for i inrange(len(js_content)):if '。' == js_content[i] or ',' ==js_content[i]:

js_content[i]= ''

elif isinstance(js_content[i], etree._Element): #音頻

res = js_content[i].xpath('//mpvoice/@voice_encode_fileid')[0]

js_content[i]= 'https://res.wx.qq.com/voice/getvoice?mediaid={}'.format(res)

vedio_list.append(js_content[i])elif 'pic' in js_content[i]: #圖片

images_list.append(js_content[i])elif 'v.qq' in js_content[i]: #視頻

vedio_json = 'https://h5vv.video.qq.com/getinfo?callback=txplayerJsonpCallBack_getinfo_24936&otype=json&vid={}' #包括視頻播放地址的json文件

url = vedio_json.format(js_content[i].split('vid=')[-1].split('&')[0])

js_content[i]=url

vedio_list.append(js_content[i])else:

js_content[i]= '

%s

' %js_content[i]

get_video(vedio_list)#從視頻、音頻json文件地址分析出下載路徑

print('-' * 30)

total_data={'title': title,'author': author,'js_name': js_name,'publish_time': publish_time,'js_content': js_content,'images': images_list,'vedios': vedio_list

}#Down(total_data) # 下載函數(shù)

def get_try(data, fangfa): #把try except封到一起

try:

res=data.xpath(fangfa)[0].strip()returnresexceptException as e:return '暫無(wú)'

def get_video(url_list): #獲取視頻 如果是音頻,直接下載,否則轉(zhuǎn)到視頻界面處理

print('獲取音、視頻路徑列表',url_list)for base_url inurl_list:if 'voice' inbase_url:pass

#voice_name = base_url.split('=')[-1][-10:]

#request.urlretrieve(base_url,'./'+voice_name+'.mp3') # 目前mp3可以下載沒(méi)有加密,后續(xù)需要轉(zhuǎn)到Down函數(shù)統(tǒng)一處理

else:print('視頻的json文件地址',base_url)

res= request.Request(base_url,headers=base_headers)

response=request.urlopen(res)

video_json= re.compile(r'txplayerJsonpCallBack_getinfo_24936\((.*)\)',re.S).search(response.read().decode()).group(1)

video_data=json.loads(video_json)

title= jsonpath.jsonpath(video_data,'$..vl.vi..ti')[0]

vid= jsonpath.jsonpath(video_data,'$..vl.vi..lnk')[0]

vkey= jsonpath.jsonpath(video_data,'$..vl.vi..fvkey')[0]

fn= jsonpath.jsonpath(video_data,'$..vl.vi..fn')[0]

url_list= jsonpath.jsonpath(video_data,'$..vl.vi..ul.ui')[0]

full_url= 'http://ugcsjy.qq.com/'+vid+'.p712.1.mp4?vkey='+vkeyprint('下載路徑',full_url)try:

base_headers['Host'] = 'ugcbsy.qq.com'v_response= requests.get(full_url,headers =base_headers)print(base_headers)print(v_response.status_code)exceptException as e:print('該下載路徑下載失敗',e)def Down(data): #先下載 下載之后還需要改路徑名稱 按時(shí)間建立文件夾

#檢測(cè)是否存在、下載、改類型

#視頻現(xiàn)在路徑有問(wèn)題,先搞定音頻和圖片名稱和路徑

for i in [data['images'],data['vedios']]:for img ini:

img_name= img.split('/')[-2][-10:] #下載后的圖片名稱

down_path= '../download/公眾號(hào)_圖片音視頻/{}'.format(img_name) #下載路徑

path= os.listdir('../download/公眾號(hào)_圖片音視頻/')

path= ','.join(path) #把當(dāng)前所需要下載的文章資源路徑拼接在一起,便于使用re.search方法判斷

if re.search(img_name + '.*', path): #正則匹配后綴名,避免判斷時(shí)因?yàn)楸镜匚募呀?jīng)改了后綴找不到

print('文件已存在', '-', img_name)else:

request.urlretrieve(img, down_path)#下載

end_name = imghdr.what(down_path) #后綴名稱

if end_name: #imghdr只能查看圖片類,視頻不能判斷,所以判斷是否為空

os.rename(down_path, down_path + '.' +end_name)print('已下載成功', '-', down_path)def Mydb(data): #下載后再存數(shù)據(jù)庫(kù)

db = pymysql.connect('127.0.0.1','root','123456','PaChong',charset='utf8')

curosr=db.cursor()

value= ','.join(['%s']*len(data))

sql= 'insert into {}({}) VALUES({})'.format(table,key,value)

data=data.values()

curosr.execute(sql,data)def main(): #主體,從這里運(yùn)行就好

start = datetime.datetime.now() #開始時(shí)間

for info in get_info(): #獲取公眾號(hào)taken fakeid等信息

get_news(info[-1]) #獲取地址

end = datetime.datetime.now() #結(jié)束時(shí)間

print('-'*30)print('總用時(shí)',end-start)print('-'*30)if __name__ == '__main__':#main()

get_news()

總結(jié)

以上是生活随笔為你收集整理的python 公众号爬虫_python_爬虫_微信公众号抓取的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。