Python selenium自動化網頁抓取器


(開開心心每一天~ ---蟲癮師)

直接入正題---Python selenium自動控制瀏覽器對網頁的數據進行抓取,其中包含按鈕點擊、跳轉頁面、搜索框的輸入、頁面的價值數據存儲、mongodb自動id標識等等等。

1、首先介紹一下 Python selenium ---自動化測試工具,用來控制瀏覽器來對網頁的操作,在爬蟲中與BeautifulSoup結合那就是天衣無縫,除去國外的一些變態的驗證網頁,對於圖片驗證碼我有自己寫的破解圖片驗證碼的源代碼,成功率在85%。

     詳情請咨詢QQ群--607021567(這不算廣告,群里有好多Python的資源分享,還有大數據的一些知識【hadoop】)

2、beautifulsoup就不需要詳細的介紹了,直接上網址-https://www.crummy.com/software/BeautifulSoup/bs4/doc/(BeautifulSoup的官方文檔)

3、關於mongodb的自動id的生成。mongodb中所有的存儲數據都是有固定的id的,但是mongodb的id對於人類來講是復雜的,對於機器來講是小菜一碟的,所以在存入數據的同時,我習慣用新id來對每一條數據的負責!

在Python中使用mongodb的話需要引進模塊 from pymongo import MongoClient,ASCENDING, DESCENDING ,該模塊就是你的責任!

 

接下來開始講程序,直接上實例(一步一步來):

引入模塊:

1 from selenium import webdriver
2 from bs4 import BeautifulSoup
3 import requests
4 from pymongo import MongoClient,ASCENDING, DESCENDING
5 import time
6 import re

其中的每一個模塊都會說已經解釋過了,其中的re、requests都是之前就有提過的,他們都是核心缺一不可!

首先,我舉一個小例子,淘寶的自動模擬搜索功能(源碼):

先說一下selenium 的定位方法

  • find_element_by_id
  • find_element_by_name
  • find_element_by_xpath
  • find_element_by_link_text
  • find_element_by_partial_link_text
  • find_element_by_tag_name
  • find_element_by_class_name
  • find_element_by_css_selector

源碼:

 1 from selenium import webdriver
 2 from bs4 import BeautifulSoup
 3 import requests
 4 from pymongo import MongoClient,ASCENDING, DESCENDING
 5 import time
 6 import re
 7 
 8 def TaoBao():
 9     try:
10         Taobaourl = 'https://www.taobao.com/'
11         driver = webdriver.Chrome()
12         driver.get(Taobaourl)
13         time.sleep(5)#通常這里需要停頓,不然你的程序很有可能被檢測到是Spider
14         text='Strong Man'#輸入的內容
15         driver.find_element_by_xpath('//input[@class="search-combobox-input"]').send_keys(text).click()
16         driver.find_element_by_xpath('//button[@class="btn-search tb-bg"]').click()
17         driver.quit()
18         
19     except Exception,e:
20         print e
21 
22 if __name__ == '__main__':
23     TaoBao()

效果的實現,你們可以直接復制后直接運行!我只用了xpath的這個方法,因為它最實在!橙色字體(如果我沒有色盲的話),就是網頁中定位的元素,可以找到的!

接下來就是與BeautifulSoup的結合了,但是我們看到的只是打開了網頁,並沒有源碼,那么就需要 “變量名.page_source”這個方法,他會實現你的夢想,你懂得?

1 ht =  driver.page_source
2 #print ht 你可以Print出啦看看
3 soup = BeautifulSoup(ht,'html.parser')

下面就是BeautifulSoup的一些語法操作了,對於數據的結構還有采集,在上一篇里面有詳細的抓取操作!!!

算了!說一個最簡單的定位抓取:

1 soup = BeautifulSoup(ht,'html.parser')
2 a = soup.find('table',id="ctl00_ContentMain_SearchResultsGrid_grid")
3 if a:  #必須加判斷,不然訪問的網頁或許沒有這一元素,程序就會都停止!

class的標簽必須是class_,一定要記住!

哈哈哈!mongodb了昂,細節細節,首先需要用到模塊----from pymongo import MongoClient,ASCENDING, DESCENDING

因為在python,mongodb的語法仍然實用,所以需要定義一個庫,並且是全局性的,還有鏈接你計算機的一個全局變量。

 

1 if __name__ == '__main__':  
2 
3   global db#全局變量                   
4   global table#全局數據庫
5   table = 'mouser_product'
6   mconn=MongoClient("mongodb://localhost")#地址
7   db=mconn.test
8   db.authenticate('test','test')#用戶名和密碼
9   Taobao()

 

定義這些后,需要我們的新id來對數據的跟蹤加定義:

1 db.sn.find_and_modify({"_id": table}, update={ "$inc": {'currentIdValue': 1}},upsert=True)
2 dic = db.ids.find({"_id":table}).limit(1)
3 return dic[0].get("currentIdValue") 

這個方法是通用的,所以只要記住其中的mongodb的語法就可以了!因為這里是有返回值的,所以這個是個方法體,這里不需要太過於糾結是怎么實現的,理解就好,中心還是在存數據的過程中

1 count = db[table].find({'數據':數據}).count() #是檢索數據庫中的數據
2 if count <= 0:                               #判斷是否有
3     ids= getNewsn()                          #ids就是我們新定義的id,這里的id是1開始的增長型id
4     db[table].insert({"ids":ids,"數據":數據})            

這樣我們的數據就直接存入到mongodb的數據庫中了,這里解釋一下為什么在大數據中這么喜歡mongodb,因為它小巧,速度佳!

最后來一個實例源碼:

 1 from selenium import webdriver
 2 from bs4 import BeautifulSoup
 3 import requests
 4 from pymongo import MongoClient,ASCENDING, DESCENDING
 5 import time
 6 import re
 7 def parser():
 8     try:
 9         f = open('sitemap.txt','r')
10         for i in  f.readlines():
11             sorturl=i.strip()
12             driver = webdriver.Firefox()
13             driver.get(sorturl)
14             time.sleep(50)
15             ht =  driver.page_source
16             #pageurl(ht)
17             soup = BeautifulSoup(ht,'html.parser')
18             a = soup.find('a',class_="first-last")
19             if a:
20                 pagenum = int(a.get_text().strip())
21                 print pagenum
22                 for i in xrange(1,pagenum):
23                     element = driver.find_element_by_xpath('//a[@id="ctl00_ContentMain_PagerTop_%s"]' %i)
24                     element.click()
25                     html =  element.page_source
26                     pageurl(html)
27                     time.sleep(50)
28                     driver.quit()
29     except Exception,e:
30         print e
31 def pageurl(ht):
32     try:
33         soup = BeautifulSoup(ht,'html.parser')
34         a = soup.find('table',id="ctl00_ContentMain_SearchResultsGrid_grid")
35         if a:
36             tr = a.find_all('tr',class_="SearchResultsRowOdd")
37             if tr:
38                     for i in tr:
39                         td = i.find_all('td')
40                         if td:
41                             url = td[2].find('a')
42                             if url:
43                                 producturl = '網址'+url['href']
44                                 print producturl
45                                 count = db[table].find({"url":producturl}).count()
46                                 if count<=0:
47                                     sn = getNewsn()
48                                     db[table].insert({"sn":sn,"url":producturl})
49                                     print str(sn) + ' inserted successfully'
50                                     time.sleep(3)
51                                 else:
52                                     print 'exists url'
53             tr1 = a.find_all('tr',class_="SearchResultsRowEven")
54             if tr1:
55                     for i in tr1:
56                         td = i.find_all('td')
57                         if td:
58                             url = td[2].find('a')
59                             if url:
60                                 producturl = '網址'+url['href']
61                                 print producturl
62                                 count = db[table].find({"url":producturl}).count()
63                                 if count<=0:
64                                     sn = getNewsn()
65                                     db[table].insert({"sn":sn,"url":producturl})
66                                     print str(sn) + ' inserted successfully'
67                                     time.sleep(3)
68                                 else:
69                                     print 'exists url'
70                                 #time.sleep(5)
71 
72     except Exception,e:
73         print e
74 def getNewsn(): 
75     db.sn.find_and_modify({"_id": table}, update={ "$inc"{'currentIdValue': 1}},upsert=True)
76     dic = db.sn.find({"_id":table}).limit(1)
77     return dic[0].get("currentIdValue")
78 
79 if __name__ == '__main__':  
80 
81   global db                    
82   global table
83   table = 'mous_product'
84   mconn=MongoClient("mongodb://localhost")
85   db=mconn.test
86   db.authenticate('test','test')
87   parser()

這一串代碼是破解一個老外的無聊驗證碼界面結緣的,我真的對他很無語了!破解方法還是實踐中!這是完整的源碼,無刪改的哦!純手工!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM