国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

python使用webdriver爬取微信公眾號(hào)

2020-02-15 22:52:40
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例為大家分享了python使用webdriver爬取微信公眾號(hào)的具體代碼,供大家參考,具體內(nèi)容如下

# -*- coding: utf-8 -*-from selenium import webdriverimport timeimport jsonimport requestsimport reimport random#微信公眾號(hào)賬號(hào)user=""#公眾號(hào)密碼password=""#設(shè)置要爬取的公眾號(hào)列表gzlist=['香河微服務(wù)']#登錄微信公眾號(hào),獲取登錄之后的cookies信息,并保存到本地文本中def weChat_login():  #定義一個(gè)空的字典,存放cookies內(nèi)容  post={}  #用webdriver啟動(dòng)谷歌瀏覽器  print("啟動(dòng)瀏覽器,打開(kāi)微信公眾號(hào)登錄界面")  driver = webdriver.Chrome(executable_path='E://program//chromedriver.exe')  #打開(kāi)微信公眾號(hào)登錄頁(yè)面  driver.get('https://mp.weixin.qq.com/')  #等待5秒鐘  time.sleep(5)  print("正在輸入微信公眾號(hào)登錄賬號(hào)和密碼......")  #清空賬號(hào)框中的內(nèi)容  driver.find_element_by_xpath("http://*[@id=/"header/"]/div[2]/div/div/form/div[1]/div[1]/div/span/input").clear()  #自動(dòng)填入登錄用戶名  driver.find_element_by_xpath("http://*[@id=/"header/"]/div[2]/div/div/form/div[1]/div[1]/div/span/input").send_keys(user)  #清空密碼框中的內(nèi)容  driver.find_element_by_xpath("http://*[@id=/"header/"]/div[2]/div/div/form/div[1]/div[2]/div/span/input").clear()  #自動(dòng)填入登錄密碼  driver.find_element_by_xpath("http://*[@id=/"header/"]/div[2]/div/div/form/div[1]/div[2]/div/span/input").send_keys(password)  # 在自動(dòng)輸完密碼之后需要手動(dòng)點(diǎn)一下記住我  print("請(qǐng)?jiān)诘卿浗缑纥c(diǎn)擊:記住賬號(hào)")  time.sleep(10)  #自動(dòng)點(diǎn)擊登錄按鈕進(jìn)行登錄  driver.find_element_by_xpath("http://*[@id=/"header/"]/div[2]/div/div/form/div[4]/a").click()  # 拿手機(jī)掃二維碼!  print("請(qǐng)拿手機(jī)掃碼二維碼登錄公眾號(hào)")  time.sleep(20)  print("登錄成功")  #重新載入公眾號(hào)登錄頁(yè),登錄之后會(huì)顯示公眾號(hào)后臺(tái)首頁(yè),從這個(gè)返回內(nèi)容中獲取cookies信息  driver.get('https://mp.weixin.qq.com/')  #獲取cookies  cookie_items = driver.get_cookies()  #獲取到的cookies是列表形式,將cookies轉(zhuǎn)成json形式并存入本地名為cookie的文本中  for cookie_item in cookie_items:    post[cookie_item['name']] = cookie_item['value']  cookie_str = json.dumps(post)  with open('cookie.txt', 'w+') as f:    f.write(cookie_str)  print("cookies信息已保存到本地")#爬取微信公眾號(hào)文章,并存在本地文本中def get_content(query):  #query為要爬取的公眾號(hào)名稱  #公眾號(hào)主頁(yè)  url = 'https://mp.weixin.qq.com'  #設(shè)置headers  header = {    "HOST": "mp.weixin.qq.com",    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0"  }  #讀取上一步獲取到的cookies  with open('cookie.txt', 'r') as f:    cookie = f.read().decode("UTF-8")  cookies = json.loads(cookie)  #登錄之后的微信公眾號(hào)首頁(yè)url變化為:https://mp.weixin.qq.com/cgi-bin/home?t=home/index&lang=zh_CN&token=1849751598,從這里獲取token信息  response = requests.get(url=url, cookies=cookies)  token = re.findall(r'token=(/d+)', str(response.url))[0]  #搜索微信公眾號(hào)的接口地址  search_url = 'https://mp.weixin.qq.com/cgi-bin/searchbiz?'  #搜索微信公眾號(hào)接口需要傳入的參數(shù),有三個(gè)變量:微信公眾號(hào)token、隨機(jī)數(shù)random、搜索的微信公眾號(hào)名字  query_id = {    'action': 'search_biz',    'token' : token,    'lang': 'zh_CN',    'f': 'json',    'ajax': '1',    'random': random.random(),    'query': query,    'begin': '0',    'count': '5'  }  #打開(kāi)搜索微信公眾號(hào)接口地址,需要傳入相關(guān)參數(shù)信息如:cookies、params、headers  search_response = requests.get(search_url, cookies=cookies, headers=header, params=query_id)  #取搜索結(jié)果中的第一個(gè)公眾號(hào)  lists = search_response.json().get('list')[0]  #獲取這個(gè)公眾號(hào)的fakeid,后面爬取公眾號(hào)文章需要此字段  fakeid = lists.get('fakeid')  #微信公眾號(hào)文章接口地址  appmsg_url = 'https://mp.weixin.qq.com/cgi-bin/appmsg?'  #搜索文章需要傳入幾個(gè)參數(shù):登錄的公眾號(hào)token、要爬取文章的公眾號(hào)fakeid、隨機(jī)數(shù)random  query_id_data = {    'token': token,    'lang': 'zh_CN',    'f': 'json',    'ajax': '1',    'random': random.random(),    'action': 'list_ex',    'begin': '0',#不同頁(yè),此參數(shù)變化,變化規(guī)則為每頁(yè)加5    'count': '5',    'query': '',    'fakeid': fakeid,    'type': '9'  }  #打開(kāi)搜索的微信公眾號(hào)文章列表頁(yè)  appmsg_response = requests.get(appmsg_url, cookies=cookies, headers=header, params=query_id_data)  #獲取文章總數(shù)  max_num = appmsg_response.json().get('app_msg_cnt')  #每頁(yè)至少有5條,獲取文章總的頁(yè)數(shù),爬取時(shí)需要分頁(yè)爬  num = int(int(max_num) / 5)  #起始頁(yè)begin參數(shù),往后每頁(yè)加5  begin = 0  while num + 1 > 0 :    query_id_data = {      'token': token,      'lang': 'zh_CN',      'f': 'json',      'ajax': '1',      'random': random.random(),      'action': 'list_ex',      'begin': '{}'.format(str(begin)),      'count': '5',      'query': '',      'fakeid': fakeid,      'type': '9'    }    print('正在翻頁(yè):--------------',begin)    #獲取每一頁(yè)文章的標(biāo)題和鏈接地址,并寫(xiě)入本地文本中    query_fakeid_response = requests.get(appmsg_url, cookies=cookies, headers=header, params=query_id_data)    fakeid_list = query_fakeid_response.json().get('app_msg_list')    for item in fakeid_list:      content_link=item.get('link')      content_title=item.get('title')      fileName=query+'.txt'      # with open('e://xhwfw.txt','a') as fh:        # fh.write(content_title+":/n"+content_link+"/n")      print content_title+":/n"+content_link+"/n"    num -= 1    begin = int(begin)    begin+=5    time.sleep(2)if __name__=='__main__':  try:    #登錄微信公眾號(hào),獲取登錄之后的cookies信息,并保存到本地文本中    weChat_login()    #登錄之后,通過(guò)微信公眾號(hào)后臺(tái)提供的微信公眾號(hào)文章接口爬取文章    for query in gzlist:      #爬取微信公眾號(hào)文章,并存在本地文本中      print("開(kāi)始爬取公眾號(hào):"+query)      get_content(query)      print("爬取完成")  except Exception as e:    print(str(e))            
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 鹤岗市| 栾城县| 资溪县| 合作市| 新建县| 雅江县| 铜陵市| 阿城市| 洪雅县| 塔城市| 寿光市| 德庆县| 平度市| 壶关县| 新巴尔虎左旗| 田林县| 吴堡县| 柏乡县| 大石桥市| 灯塔市| 元阳县| 龙泉市| 和田县| 北流市| 祁门县| 甘洛县| 定襄县| 望奎县| 赣州市| 昭通市| 剑河县| 同德县| 平安县| 淮阳县| 老河口市| 黄石市| 平南县| 开远市| 杨浦区| 莒南县| 宜良县|