如何編寫 Python 程式爬取新浪軍事論壇?

來源:互聯網
上載者:User

回複內容:

context_re = r'(.*?)
'
你準備的這個Regex啊,truncated!斷在了
這裡,所以只能爬第一段。


爬取新浪軍事論壇需要做三件事:

一、

上CSDN汪海老師的專欄,http://blog.csdn.net/column/details/why-bug.html ,學習一個。


二、

按F12看一下前端。


三、

from bs4 import BeautifulSoupimport requestsresponse = requests.get("http://club.mil.news.sina.com.cn/thread-666013-1-1.html?retcode=0") #硬點網址response.encoding = 'gb18030' #中文編碼soup = BeautifulSoup(response.text, 'html.parser') #構建BeautifulSoup對象divs = soup('div', 'mainbox') #每個樓層for div in divs:    comments = div.find_all('div','cont f14') #每個樓層的本文            with open('Sina_Military_Club.txt','a') as f:        f.write('\n'+str(comments)+'\n')
剛好幾個小時前就在寫一個爬取網站會員(公司)資料的小程式
具體的編程問題就不回答了,跟用什麼語言寫代碼無關,關鍵是你要分析好這個頁面的html代碼結構,寫出合適的Regex來進行匹配,如果想簡化的話,可以進行分次匹配(比如先得到裡面的第一個裡面的內容就是原帖的地址,然後再進一步處理)
大資料分析就不會了,還請賜教。
import requestsfrom bs4 import BeautifulSoupr = requests.get("http://club.mil.news.sina.com.cn/thread-666013-1-1.html")r.encoding = r.apparent_encodingsoup = BeautifulSoup(r.text)result = soup.find(attrs={"class": "cont f14"})print result.text
用beautifulSoup吧,正則太多了看著都頭疼.先用了BeautifulSoup爬取資料
# -*- coding:utf-8 -*-import re, requestsfrom bs4 import BeautifulSoupimport sysreload(sys)sys.setdefaultencoding('utf-8')url = "http://club.mil.news.sina.com.cn/viewthread.php?tid=666013&extra=page%3D1&page=1"req = requests.get(url)req.encoding = req.apparent_encodinghtml = req.textsoup = BeautifulSoup(html)file = open('sina_club.txt', 'w')x = 1for tag in soup.find_all('div', attrs = {'class': "cont f14"}):    word = tag.get_text()    line1 = "---------------評論" + str(x) + "---------------------" + "\n"    line2 = word + "\n"    line = line1 + line2    x += 1    file.write(line)file.close()
哎,扒就扒吧,發了paper能不能告訴我刊號頁數讓我看一下?我們自己都沒做大資料分析……建議用一下正則測試載入器 你需要pyquery,可以使用jquery一樣的文法。你值得擁有。
https://pythonhosted.org/pyquery/
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.