python3之微信文章爬蟲實(shí)例講解
前提:
python3.4
windows
作用:通過搜狗的微信搜索接口http://weixin.sogou.com/來搜索相關(guān)微信文章,并將標(biāo)題及相關(guān)鏈接導(dǎo)入Excel表格中
說明:需xlsxwriter模塊,另程序編寫時(shí)間為2017/7/11,以免之后程序無法使用可能是網(wǎng)站做過相關(guān)改變,程序較為簡單,除去注釋40多行。
正題:
思路:打開初始Url --> 正則獲取標(biāo)題及鏈接 --> 改變page循環(huán)第二步 --> 將得到的標(biāo)題及鏈接導(dǎo)入Excel
爬蟲的第一步都是先手工操作一遍(閑話)
進(jìn)入上面提到的網(wǎng)址,如輸入:“圖片識(shí)別”,搜索,網(wǎng)址變?yōu)椤癶ttp://weixin.sogou.com/weixin?type=2&query=%E5%9B%BE%E7%89%87%E8%AF%86%E5%88%AB&ie=utf8&s_from=input&_sug_=n&_sug_type_=1&w=01015002&oq=&ri=4&sourceid=sugg&sut=0&sst0=1499778531195&lkt=0%2C0%2C0&p=40040108”標(biāo)紅為重要參數(shù),type=1時(shí)是搜索公眾號(hào),暫且不管,query=‘搜索關(guān)鍵詞',關(guān)鍵詞已經(jīng)被編碼,還有一個(gè)隱藏參數(shù)page=1
當(dāng)你跳到第二頁時(shí)可以看到“http://weixin.sogou.com/weixin?oq=&query=%E5%9B%BE%E7%89%87%E8%AF%86%E5%88%AB&_sug_type_=1&sut=0&lkt=0%2C0%2C0&s_from=input&ri=4&_sug_=n&type=2&sst0=1499778531195&page=2&ie=utf8&p=40040108&dp=1&w=01015002&dr=1”
好了,url可以得到了
url = 'http://weixin.sogou.com/weixin?type=2&query='+search+'&page='+str(page)
search是要搜索的關(guān)鍵詞,用quote()編碼即可插入
search = urllib.request.quote(search)
page是用來循環(huán)的
for page in range(1,pagenum+1): url = 'http://weixin.sogou.com/weixin?type=2&query='+search+'&page='+str(page)
完整的url已經(jīng)得到了,接下來訪問url,獲得其中的數(shù)據(jù)(創(chuàng)建opener對象,添加header())
import urllib.request header = ('User-Agent','Mozilla/5.0') opener = urllib.request.build_opener() opener.addheaders = [header] urllib.request.install_opener(opener) data = urllib.request.urlopen(url).read().decode()
得到頁面內(nèi)容,采用正則表達(dá)獲取相關(guān)數(shù)據(jù)
import re finddata = re.compile('<a target="_blank" href="(.*?)" rel="external nofollow" rel="external nofollow" .*?uigs="article_title_.*?">(.*?)</a>').findall(data) #finddata = [('',''),('','')]
通過正則獲取的數(shù)據(jù)中存在干擾項(xiàng)(鏈接:‘a(chǎn)mp;')和無關(guān)項(xiàng)(標(biāo)題:'<em><...><....></em>'),用replace()解決
title = title.replace('<em><!--red_beg-->','') title = title.replace('<!--red_end--></em>','') link = link.replace('amp;','')
將處理后的標(biāo)題和鏈接保存在列表中
title_link.append(link) title_link.append(title)
如此搜索的標(biāo)題和鏈接都得到了,接下來導(dǎo)入Excel
先創(chuàng)建Excel
import xlsxwriter workbook = xlsxwriter.Workbook(search+'.xlsx') worksheet = workbook.add_worksheet('微信')
將title_link中的數(shù)據(jù)導(dǎo)入Excel
for i in range(0,len(title_link),2): worksheet.write('A'+str(i+1),title_link[i+1]) worksheet.write('C'+str(i+1),title_link[i]) workbook.close()
完整代碼:
''' python3.4 + windows 羽凡-2017/7/11- 用于搜索微信文章,保存標(biāo)題及鏈接至Excel中 每個(gè)頁面10秒延遲,防止被限制 import urllib.request,xlsxwriter,re,time ''' import urllib.request search = str(input("搜索微信文章:")) pagenum = int(input('搜索頁數(shù):')) import xlsxwriter workbook = xlsxwriter.Workbook(search+'.xlsx') search = urllib.request.quote(search) title_link = [] for page in range(1,pagenum+1): url = 'http://weixin.sogou.com/weixin?type=2&query='+search+'&page='+str(page) import urllib.request header = ('User-Agent','Mozilla/5.0') opener = urllib.request.build_opener() opener.addheaders = [header] urllib.request.install_opener(opener) data = urllib.request.urlopen(url).read().decode() import re finddata = re.compile('<a target="_blank" href="(.*?)" rel="external nofollow" rel="external nofollow" .*?uigs="article_title_.*?">(.*?)</a>').findall(data) #finddata = [('',''),('','')] for i in range(len(finddata)): title = finddata[i][1] title = title.replace('<em><!--red_beg-->','') title = title.replace('<!--red_end--></em>','') try: #標(biāo)題中可能存在引號(hào) title = title.replace('“','"') title = title.replace('”','"') except: pass link = finddata[i][0] link = link.replace('amp;','') title_link.append(link) title_link.append(title) print('第'+str(page)+'頁') import time time.sleep(10) worksheet = workbook.add_worksheet('微信') worksheet.set_column('A:A',70) worksheet.set_column('C:C',100) bold = workbook.add_format({'bold':True}) worksheet.write('A1','標(biāo)題',bold) worksheet.write('C1','鏈接',bold) for i in range(0,len(title_link),2): worksheet.write('A'+str(i+1),title_link[i+1]) worksheet.write('C'+str(i+1),title_link[i]) workbook.close() print('導(dǎo)入Excel完畢!')
以上這篇python3之微信文章爬蟲實(shí)例講解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- python爬蟲_微信公眾號(hào)推送信息爬取的實(shí)例
- Python爬蟲實(shí)現(xiàn)“盜取”微信好友信息的方法分析
- Python 微信爬蟲完整實(shí)例【單線程與多線程】
- python3簡單實(shí)現(xiàn)微信爬蟲
- python爬蟲使用正則爬取網(wǎng)站的實(shí)現(xiàn)
- Python爬蟲之爬取淘女郎照片示例詳解
- Python爬蟲實(shí)例——爬取美團(tuán)美食數(shù)據(jù)
- Python爬蟲實(shí)例——scrapy框架爬取拉勾網(wǎng)招聘信息
- Python爬蟲爬取百度搜索內(nèi)容代碼實(shí)例
- python爬蟲開發(fā)之使用python爬蟲庫requests,urllib與今日頭條搜索功能爬取搜索內(nèi)容實(shí)例
- python爬蟲爬取筆趣網(wǎng)小說網(wǎng)站過程圖解
- Python爬蟲爬取微信朋友圈
相關(guān)文章
Python啟動(dòng)UDP服務(wù),監(jiān)聽并接收客戶端數(shù)據(jù)方式
這篇文章主要介紹了Python啟動(dòng)UDP服務(wù),監(jiān)聽并接收客戶端數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07利用Python實(shí)現(xiàn)從PDF到CSV的轉(zhuǎn)換
將PDF轉(zhuǎn)換為CSV極大地提升了數(shù)據(jù)的實(shí)用價(jià)值,Python作為一種強(qiáng)大的編程語言,能夠高效完成這一轉(zhuǎn)換任務(wù),本文將介紹如何利用Python實(shí)現(xiàn)從PDF到CSV的轉(zhuǎn)換,需要的朋友可以參考下2024-07-07利用scrapy將爬到的數(shù)據(jù)保存到mysql(防止重復(fù))
這篇文章主要給大家介紹了關(guān)于利用scrapy將爬到的數(shù)據(jù)保存到mysql(防止重復(fù))的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2018-03-03關(guān)于TensorBoard的使用以及遇到的坑記錄
這篇文章主要介紹了關(guān)于TensorBoard的使用以及遇到的坑記錄,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09