python3之微信文章爬蟲實例講解
前提:
python3.4
windows
作用:通過搜狗的微信搜索接口http://weixin.sogou.com/來搜索相關(guān)微信文章,并將標題及相關(guān)鏈接導入Excel表格中
說明:需xlsxwriter模塊,另程序編寫時間為2017/7/11,以免之后程序無法使用可能是網(wǎng)站做過相關(guān)改變,程序較為簡單,除去注釋40多行。
正題:
思路:打開初始Url --> 正則獲取標題及鏈接 --> 改變page循環(huán)第二步 --> 將得到的標題及鏈接導入Excel
爬蟲的第一步都是先手工操作一遍(閑話)
進入上面提到的網(wǎng)址,如輸入:“圖片識別”,搜索,網(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”標紅為重要參數(shù),type=1時是搜索公眾號,暫且不管,query=‘搜索關(guān)鍵詞',關(guān)鍵詞已經(jīng)被編碼,還有一個隱藏參數(shù)page=1
當你跳到第二頁時可以看到“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)容,采用正則表達獲取相關(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ù)中存在干擾項(鏈接:‘a(chǎn)mp;')和無關(guān)項(標題:'<em><...><....></em>'),用replace()解決
title = title.replace('<em><!--red_beg-->','')
title = title.replace('<!--red_end--></em>','')
link = link.replace('amp;','')
將處理后的標題和鏈接保存在列表中
title_link.append(link) title_link.append(title)
如此搜索的標題和鏈接都得到了,接下來導入Excel
先創(chuàng)建Excel
import xlsxwriter
workbook = xlsxwriter.Workbook(search+'.xlsx')
worksheet = workbook.add_worksheet('微信')
將title_link中的數(shù)據(jù)導入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-
用于搜索微信文章,保存標題及鏈接至Excel中
每個頁面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:
#標題中可能存在引號
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','標題',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('導入Excel完畢!')
以上這篇python3之微信文章爬蟲實例講解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- python爬蟲_微信公眾號推送信息爬取的實例
- Python爬蟲實現(xiàn)“盜取”微信好友信息的方法分析
- Python 微信爬蟲完整實例【單線程與多線程】
- python3簡單實現(xiàn)微信爬蟲
- python爬蟲使用正則爬取網(wǎng)站的實現(xiàn)
- Python爬蟲之爬取淘女郎照片示例詳解
- Python爬蟲實例——爬取美團美食數(shù)據(jù)
- Python爬蟲實例——scrapy框架爬取拉勾網(wǎng)招聘信息
- Python爬蟲爬取百度搜索內(nèi)容代碼實例
- python爬蟲開發(fā)之使用python爬蟲庫requests,urllib與今日頭條搜索功能爬取搜索內(nèi)容實例
- python爬蟲爬取筆趣網(wǎng)小說網(wǎng)站過程圖解
- Python爬蟲爬取微信朋友圈
相關(guān)文章
python實現(xiàn)圖書館搶座(自動預約)功能的示例代碼
這篇文章主要介紹了python實現(xiàn)圖書館搶座(自動預約)功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
Python 實現(xiàn)判斷圖片格式并轉(zhuǎn)換,將轉(zhuǎn)換的圖像存到生成的文件夾中
今天小編就為大家分享一篇Python判斷圖片格式并轉(zhuǎn)換,將轉(zhuǎn)換的圖像存到生成的文件夾中,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
Python的凈值數(shù)據(jù)接口調(diào)用示例分享
這篇文章主要介紹了Python的凈值數(shù)據(jù)接口調(diào)用示例分享的相關(guān)資料,需要的朋友可以參考下2016-03-03
python?tkinter自定義實現(xiàn)Expander控件
和其他成熟的GUI庫相比,tkinter的組件并不是太多,但在自定義組件這一點上,并不遜色于其他框架,下面小編就教大家如何自定義一個Expander控件吧2023-08-08
通過實例解析python subprocess模塊原理及用法
這篇文章主要介紹了通過實例解析python subprocess模塊原理及用法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-10-10

