python3爬取微信文章

來源:互聯網
上載者:User
前提:

python3.4

windows

作用:通過搜狗的搜尋介面來搜尋相關文章,並將標題及相關連結匯入Excel表格中

說明:需xlsxwriter模組,另程式編寫時間為2017/7/11,以免之後程式無法使用可能是網站做過相關改變,程式較為簡單,除去注釋40多行。

正題:

思路:開啟初始Url --> 正則擷取標題及連結 --> 改變page迴圈第二步 --> 將得到的標題及連結匯入Excel

爬蟲的第一步都是先手工操作一遍(閑話)

進入上面提到的網址,如輸入:“圖片識別”,搜尋,網址變為“”標紅為重要參數,type=1時是搜尋公眾號,暫且不管,query=‘搜尋關鍵詞’,關鍵詞已經被編碼,還有一個隱藏參數page=1

當你跳到第二頁時可以看到“”

好了,url可以得到了

1 url = 'http://weixin.sogou.com/weixin?type=2&query='+search+'&page='+str(page)

search是要搜尋的關鍵詞,用quote()編碼即可插入

1 search = urllib.request.quote(search)

page是用來迴圈的

1 for page in range(1,pagenum+1):2     url = 'http://weixin.sogou.com/weixin?type=2&query='+search+'&page='+str(page)

完整的url已經得到了,接下來訪問url,獲得其中的資料(建立opener對象,添加header())

1 import urllib.request2     header = ('User-Agent','Mozilla/5.0')3     opener = urllib.request.build_opener()4     opener.addheaders = [header]5     urllib.request.install_opener(opener)6     data = urllib.request.urlopen(url).read().decode()

得到頁面內容,採用正則表達擷取相關資料

1 import re2     finddata = re.compile('<a target="_blank" href="(.*?)".*?uigs="article_title_.*?">(.*?)</a>').findall(data)3     #finddata = [('',''),('','')]

通過正則擷取的資料中存在幹擾項(連結:‘amp;’)和無關項(標題:'<em><...><....></em>'),用replace()解決

1 title = title.replace('<em><!--red_beg-->','')2 title = title.replace('<!--red_end--></em>','')
1 link = link.replace('amp;','')

將處理後的標題和連結儲存在列表中

1 title_link.append(link)2 title_link.append(title)

如此搜尋的標題和連結都得到了,接下來匯入Excel

先建立Excel

1 import xlsxwriter2 workbook = xlsxwriter.Workbook(search+'.xlsx')
3 worksheet = workbook.add_worksheet('')

將title_link中的資料匯入Excel

1 for i in range(0,len(title_link),2):2     worksheet.write('A'+str(i+1),title_link[i+1])3     worksheet.write('C'+str(i+1),title_link[i])4 workbook.close()

完整代碼:

 1 ''' 2 python3.4 + windows 3 羽凡-2017/7/11- 4 用於搜尋文章,儲存標題及連結至Excel中 5 每個頁面10秒延遲,防止被限制 6 import urllib.request,xlsxwriter,re,time 7 ''' 8 import urllib.request 9 search = str(input("搜尋文章:"))10 pagenum = int(input('搜尋網頁數:'))11 import xlsxwriter12 workbook = xlsxwriter.Workbook(search+'.xlsx')13 search = urllib.request.quote(search)14 title_link = []15 for page in range(1,pagenum+1):16     url = 'http://weixin.sogou.com/weixin?type=2&query='+search+'&page='+str(page)17     import urllib.request18     header = ('User-Agent','Mozilla/5.0')19     opener = urllib.request.build_opener()20     opener.addheaders = [header]21     urllib.request.install_opener(opener)22     data = urllib.request.urlopen(url).read().decode()23     import re24     finddata = re.compile('<a target="_blank" href="(.*?)".*?uigs="article_title_.*?">(.*?)</a>').findall(data)25     #finddata = [('',''),('','')]26     for i in range(len(finddata)):27         title = finddata[i][1]28         title = title.replace('<em><!--red_beg-->','')29         title = title.replace('<!--red_end--></em>','')30         try:31             #標題中可能存在引號32             title = title.replace('&ldquo;','"')33             title = title.replace('&rdquo;','"')34         except:35             pass36         link = finddata[i][0]37         link = link.replace('amp;','')38         title_link.append(link)39         title_link.append(title)40     print('第'+str(page)+'頁')41     import time42     time.sleep(10)43 worksheet = workbook.add_worksheet('')44 worksheet.set_column('A:A',70)45 worksheet.set_column('C:C',100)46 bold = workbook.add_format({'bold':True})47 worksheet.write('A1','標題',bold)48 worksheet.write('C1','連結',bold)49 for i in range(0,len(title_link),2):50     worksheet.write('A'+str(i+1),title_link[i+1])51     worksheet.write('C'+str(i+1),title_link[i])52 workbook.close()53 print('匯入Excel完畢!')

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.