小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

基于Python +Selenium的爬蟲詳解

 風(fēng)聲之家 2019-07-19

一.背景

1. Selenium

Selenium 是一個用于web應(yīng)用程序自動化測試的工具,直接運行在瀏覽器當中,支持chrome、firefox等主流瀏覽器??梢酝ㄟ^代碼控制與頁面上元素進行交互(點擊、輸入等),也可以獲取指定元素的內(nèi)容。

2.優(yōu)劣

劣勢:

相比于抓包→構(gòu)造請求→解析返回值的爬蟲,由于Selenium需要生成一個瀏覽器環(huán)境,所有操作(與元素交互、獲取元素內(nèi)容等)均需要等待頁面加載完畢后才可以繼續(xù)進行,所以速度相比構(gòu)造請求的慢很多。

對于為了反爬做了特殊處理的展示內(nèi)容,如字體加密(參考貓眼)、圖片替換數(shù)字(參考自如)等,可能取不到想要的數(shù)據(jù)。

使用圖片替換數(shù)字的自如:

image-20190107215702089

優(yōu)勢:

  • 不需要做復(fù)雜的抓包、構(gòu)造請求、解析數(shù)據(jù)等,開發(fā)難度相對要低一些。

  • 其訪問參數(shù)跟使用瀏覽器的正常用戶一模一樣,訪問行為也相對更像正常用戶,不容易被反爬蟲策略命中。

  • 生成的瀏覽器環(huán)境可以自動運行 JS 文件,所以不用擔心如何逆向混淆過的JS文件生成用作人機校驗的參數(shù),如馬蜂窩酒店評論的人機校驗參數(shù)_sn,網(wǎng)易云音樂評論的人機校驗參數(shù)params、encSecKey??梢宰孕凶グ榭础?/span>

  • 如果需要抓取同一個前端頁面上面來自不同后端接口的信息,如OTA酒店詳情頁的酒店基礎(chǔ)信息、價格、評論等,使用Selenium可以在一次請求中同時完成對三個接口的調(diào)用,相對方便。

二、實現(xiàn)

1.環(huán)境

python3.6 + Macos

2.依賴包

Selenium

安裝的時候是大寫的 S ,import的時候是 小寫 s。

pip install Selenium

3.瀏覽器驅(qū)動(webdriver)

加載瀏覽器環(huán)境需要下載對應(yīng)的瀏覽器驅(qū)動,此處選擇 Chrome。

下載地址:http://npm./mirrors/chromedriver/ ,

選擇合適的版本下載解壓后放在隨便一個位置即可。

4.hello world

from selenium import webdriver

'''這里填剛剛下載的驅(qū)動的路徑'''
path = '/Applications/Google Chrome.app/Contents/chromedriver'

driver = webdriver.Chrome(executable_path=path)

url = 'http://hotel.qunar.com/city/beijing_city/'
driver.get(url)

'''運行上述代碼,會打開一個瀏覽器,并且加載去哪兒的酒店列表頁'''

這時候可以通過webdriver自帶的一些的一些方法獲取元素內(nèi)容或者與元素進行交互。

image-20190108224445170

#返回ID = js_block_beijing_city_7810的元素信息
hotel_info = driver.find_element_by_id('js_block_beijing_city_7810')
print(hotel_info.text)
#返回 展示在列表頁的酒店信息
#同理,可以find_element_by_[class_name|name] 等,均可完成查詢。

也可以通過方法 find_elements查找符合某條件的一組元素,以列表的形式返回。

image-20190108225039418

#當需要查詢的唯一標識帶有空格時,可以使用find_elements_by_css_selector,否則會報錯。
hotel_list = driver.find_elements_by_css_selector("[class='b_result_box js_list_block  b_result_commentbox']")
print(hotel_list)
#返回酒店列表的全部信息。

5.關(guān)閉圖片加載

在不需要抓取圖片的情況下,可以設(shè)置不加載圖片,節(jié)約時間,這樣屬于調(diào)整本地設(shè)置,在傳參上并不會有異常。

from selenium import webdriver

chrome_opt = webdriver.ChromeOptions()
prefs={"profile.managed_default_content_settings.images":2}
chrome_opt.add_experimental_option("prefs",prefs)

path = '' #驅(qū)動路徑
browser_noPic = webdriver.Chrome(executable_path=path,chrome_options=chrome_opt)

三、使用webdriver與元素進行交互

1.模擬鼠標點擊

image-20190108230139238

hotel_info = driver.find_element_by_id("js_plugin_tag_beijing_city_7810")
hotel.info.click()
#進入酒店詳情頁

2.模擬鍵盤輸入

hotel_search = driver.find_element_by_id("jxQ")
hotel_search.send_keys("如")
hotel_search.send_keys("如家")
#由于搜索框輸入的第一個字會被選中,所以需要第二次才能完整輸入,當然也可以模擬按鍵盤的 →(右鍵)取消選中后再次輸入。

3.模擬下拉

webdriver中對鼠標的操作的方法封裝在ActionChains類中 ,使用前要先導(dǎo)入ActionChains類:

from selenium.webdriver.common.action_chains import  ActionChains

"""在頁面頂部、底部個找了一個元素,并模擬鼠標從頂?shù)降椎幕瑒?quot;""
start = driver.find_element_by_class_name('e_above_header')
target = driver.find_element_by_class_name('qn_footer')
ActionChains(driver).drag_and_drop(start,target).perform()

此外,webdiver還提供豐富的交互功能,比如鼠標懸停、雙擊、按住左鍵等等,此處不展開介紹。

四、一個完整的模擬瀏覽器爬蟲

from selenium import webdriver
from selenium.webdriver.common.action_chains import  ActionChains
import time

'''這里填剛剛下載的驅(qū)動的路徑'''
path = '/Users/./Desktop/chromedriver'
driver = webdriver.Chrome(executable_path=path)

url = 'http://hotel.qunar.com/city/beijing_city/'
driver.get(url) 


time.sleep(6#等待頁面加載完再進行后續(xù)操作

"""在頁面頂部、底部個找了一個元素,并模擬鼠標從頂?shù)降椎幕瑒?quot;""
start = driver.find_element_by_class_name('e_above_header')
target = driver.find_element_by_class_name('qn_footer')
ActionChains(driver).drag_and_drop(start,target).perform()

time.sleep(5#等待頁面加載完再進行后續(xù)操作

hotel_link_list = driver.find_elements_by_css_selector("[class='item_price js_hasprice']")
print("在此頁面共有酒店",len(hotel_link_list),"家")
windows = driver.window_handles

#此處可以爬整個頁面任何想要想要的元素
list_hotel_info=[]
def hotel_info_clawer():
    list_hotel_info.append([driver.find_element_by_class_name("info").text,
                            driver.find_element_by_class_name("js-room-table").text,
                            driver.find_element_by_class_name("dt-module").text])


for i in range(len(hotel_link_list)):
    hotel_link_list[i].click()
    driver.switch_to.window(windows[-1]) #切換到剛打開的酒店詳情頁
    hotel_info_clawer()
    driver.close() #關(guān)閉已經(jīng)爬完的酒店詳情頁    
    print("已經(jīng)抓取酒店",i,"家")



#后面可以補充翻頁繼續(xù)抓取的部分

五、使用截圖+OCR抓取關(guān)鍵數(shù)據(jù)

對于做了特殊處理的信息,如上述的貓眼電影的票房信息、自如的價格等,不適用于直接獲取制定元素的信息進行抓取,可以使用截圖+OCR的方式抓取此類數(shù)據(jù)。

以自如的房租為例:

image-20190112201939908

from selenium import webdriver

'''這里填剛剛下載的驅(qū)動的路徑'''
path = '/Applications/Google Chrome.app/Contents/chromedriver'
driver = webdriver.Chrome(executable_path=path)

url = 'http://www./z/vr/61715463.html'
driver.get(url)

price = diver.find_element_by_class_name('room_price')

print(price.text)#由于自如的價格用圖片做了替換,這樣并不能獲取到實際價格,需要獲取圖片再做ocr處理

"對指定元素部分截圖再保存"
price.screenshot('/Users/./Desktop/price.png')

安裝ocr工具:

Tesseract是一個開源的OCR引擎,能識別100多種語言(中,英,韓,日,德,法…等等),但是Tesseract對手寫的識別能力較差,僅適用于打印字體。

//僅安裝tesseract,不安裝訓(xùn)練工具和其他語音包,需要識別中文的話得額外下載
//下載地址:https://github.com/tesseract-ocr/tessdata
brew install tesseract

使用Tesseract:

tesseract ~/price.png result
//識別圖片并將結(jié)果存在result里面

在python下使用Tesseract:

首先安裝依賴包:pip install pytesseract

import pytesseract
from PIL import Image

# open image
image = Image.open('price.png')
code = pytesseract.image_to_string(image)
print(code)

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多