前提:
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對(duì)象,添加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(' (.*?) ').findall(data) #finddata = [('',''),('','')]
通過正則獲取的數(shù)據(jù)中存在干擾項(xiàng)(鏈接:‘a(chǎn)mp;')和無關(guān)項(xiàng)(標(biāo)題:' <...><....> '),用replace()解決
title = title.replace(' ','') title = title.replace(' ','') 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(' (.*?) ').findall(data) #finddata = [('',''),('','')] for i in range(len(finddata)): title = finddata[i][1] title = title.replace(' ','') title = title.replace(' ','') 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è)參考,也希望大家多多支持腳本之家。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
