石家莊政民互動數據爬取-寫在前面
今天,咱抓取一個網站,這個網站呢,涉及的內容就是 網友留言和回覆,特別簡單,可是網站是gov
的。網址爲 http://www.sjz.gov.cn/col/1490066682000/index.html
html
首先聲明,爲了學習,絕無惡意抓取信息,無論你信不信,數據我沒有長期存儲,預計存儲到重裝操做系統就刪除。git
石家莊政民互動數據爬取-網頁分析
點擊更多回復 ,能夠查看到相應的數據。 github
數據量很大14萬條,,數據爬完,還能夠用來學習數據分析,真是niceweb
通過分析以後,找到了列表頁面。 數據的爬取此次咱們採用的是
selenium
,解析頁面採用lxml
,數據存儲採用pymongo
,關於selenium
你能夠去搜索引擎搜索相關的教程,好多的,主要就是打開一個瀏覽器,而後模擬用戶的操做,你能夠去系統的學習一下。mongodb
石家莊政民互動數據爬取-擼代碼
導入必備模塊
from selenium import webdriver from selenium.common.exceptions import TimeoutException from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from lxml import etree import pymongo import time
石家莊政民互動數據爬取-打開瀏覽器,獲取總頁碼
這個操做最重要的步驟,你搜索以後就會知道,須要提早下載一個叫作 chromedriver.exe 的東東,而後把他配置好,自行解決去吧~chrome
# 加載瀏覽器引擎,須要提早下載好 chromedriver.exe 。 browser = webdriver.Chrome() wait = WebDriverWait(browser,10) def get_totle_page(): try: # 瀏覽器跳轉 browser.get("http://www.sjz.gov.cn/zfxxinfolist.jsp?current=1&wid=1&cid=1259811582187") # 等待元素加載到 totle_page = wait.until( EC.presence_of_element_located((By.CSS_SELECTOR,'input[type="hidden"]:nth-child(4)')) ) # 獲取屬性 totle = totle_page.get_attribute('value') # 獲取首頁數據,這個地方先沒必要須 ############################## #get_content() ############################## return totle except TimeoutError: return get_totle_page()
上面的代碼在測試以後,你會獲得以下結果
瀏覽器
這時候,你已經獲得20565
這個總頁碼數目了,只須要進行一系列循環的操做便可,接下來有一個重要的函數,叫作next_page
這個函數裏面,須要進行一個模擬用戶行爲的操做,輸入一個頁碼,而後點擊跳轉。多線程
def main(): totle = int(get_totle_page()) # 獲取完整頁碼 for i in range(2,totle+1): print("正在加載第{}頁數據".format(i)) # 獲取下一頁 next_page(i) if __name__ == '__main__': print(main())
輸入頁碼,點擊跳轉
def next_page(page_num): try: input = wait.until( EC.presence_of_element_located((By.CSS_SELECTOR,"#pageto")) ) submit = wait.until( EC.element_to_be_clickable((By.CSS_SELECTOR,"#goPage")) ) input.clear() # 清空文本框 input.send_keys(page_num) # 發送頁碼 submit.click() # 點擊跳轉 #get_content(page_num) except TimeoutException: next_page(page_num)
以上代碼實現的效果動態演示爲jsp
石家莊政民互動數據爬取-解析頁面
能夠進行翻頁以後,經過browser.page_source
獲取網頁源碼,網頁源碼經過lxml
進行解析。編寫相應的方法爲函數
def get_content(page_num=None): try: wait.until( EC.presence_of_element_located((By.CSS_SELECTOR, "table.tably")) ) html = browser.page_source # 獲取網頁源碼 tree = etree.HTML(html) # 解析 tables = tree.xpath("//table[@class='tably']") for table in tables: name = table.xpath("tbody/tr[1]/td[1]/table/tbody/tr[1]/td")[0].text public_time = table.xpath("tbody/tr[1]/td[1]/table/tbody/tr[2]/td")[0].text to_people = table.xpath("tbody/tr[1]/td[1]/table/tbody/tr[3]/td")[0].text content = table.xpath("tbody/tr[1]/td[2]/table/tbody/tr[1]/td")[0].text repl_time = table.xpath("tbody/tr[2]/td[1]/table/tbody/tr[1]/td")[0].text repl_depart = table.xpath("tbody/tr[2]/td[1]/table/tbody/tr[2]/td")[0].text repl_content = table.xpath("tbody/tr[2]/td[2]/table/tbody/tr[1]/td")[0].text # 清理數據 consult = { "name":name.replace("網友:",""), "public_time":public_time.replace("時間:",""), "to_people":to_people.replace("留言對象:",""), "content":content, "repl_time":repl_time.replace("時間:",""), "repl_depart":repl_depart.replace("回覆部門:",""), "repl_content":repl_content } # 數據存儲到mongo裏面 #save_mongo(consult) except Exception: # 這個地方須要特殊說明一下 print("異常錯誤X1") print("瀏覽器休息一下") time.sleep(60) browser.get("http://www.sjz.gov.cn/zfxxinfolist.jsp?current={}&wid=1&cid=1259811582187".format(page_num)) get_content()
在實際的爬取過程當中發現,通過幾百頁以後,就會限制一下IP,因此當咱們捕獲頁面信息出錯,須要暫停一下,等待頁面正常以後,在繼續爬取數據。
數據存儲到mongodb裏面
爬取到的最終數據,我存儲到了mongodb裏面,這個就沒有什麼難度了,咱們按照常規的套路編寫便可。
寫在最後
因爲此次爬取的網站是gov
的,因此建議不要用多線程,源碼也不發送到github上去了,要不惹禍,若是有任何疑問,請評論。nice boy
<div align="center"> <img src="https://user-gold-cdn.xitu.io/2018/12/24/167ddcd0b1450df3?w=259&h=287&f=png&s=57727" width=20% /> </div>