使用Selenium驅動chrome頁面,獲得淘寶信息并用BeautifulSoup分析得到結果。
使用Selenium時注意頁面的加載判斷,以及加載超時的異常處理。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
import json import re from bs4 import BeautifulSoup 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 browser = webdriver.Chrome() # 瀏覽器需要多次使用,所以單獨拿出來。設置一個最長的等待時間,等待目標加載完成 wait = WebDriverWait(browser, 10 ) def search(keyword): # wait容易出現加載時間長的問題,因此用try來捕捉異常 try : browser.get( 'https://www.taobao.com' ) # 加載需要一定時間的,設置了等待時間,等待加載 # 輸入按鈕的加載等待 input = wait.until( # 設置加載目標,它是一個選擇器,參數是需要選擇方式和等待加載的內容 EC.presence_of_element_located((By.CSS_SELECTOR, "#q" )) # 選擇CSS選擇器和選擇內容 ) # 提交按鈕 submit = wait.until( # EC后面是選擇條件,按鈕的加載條件最好的是element_to_be_clickable,意思為元素可以點擊的 EC.element_to_be_clickable((By.CSS_SELECTOR, "#J_TSearchForm > div.search-button > button" )) ) input .send_keys(keyword) # send_keys對輸入框輸入內容 submit.click() # 提交搜索內容,進入下一個頁面 # 等待頁碼元素加載完成,并返回最大頁碼數 total = wait.until( EC.presence_of_element_located((By.CSS_SELECTOR, "#mainsrp-pager > div > div > div > div.total" )) ) # 等待加載完成后獲取信息 get_products() return total.text except TimeoutException: # 超時后重新請求,因此遞歸調用 return search() def next_page(page_number): try : # 頁碼輸入框和翻頁按鈕 input = wait.until( EC.presence_of_element_located((By.CSS_SELECTOR, "#mainsrp-pager > div > div > div > div.form > input" )) ) # 提交按鈕 submit = wait.until( EC.element_to_be_clickable( (By.CSS_SELECTOR, "#mainsrp-pager > div > div > div > div.form > span.btn.J_Submit" )) ) input .clear() input .send_keys(page_number) submit.click() # 判斷翻頁成功 wait.until( EC.text_to_be_present_in_element((By.CSS_SELECTOR, '#mainsrp-pager > div > div > div > ul > li.item.active > span' ), str (page_number))) get_products() except TimeoutException: return next_page(page_number) def get_products(): # 判斷單個頁面是否被加載出來 wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#mainsrp-itemlist .items .item' ))) html = browser.page_source # 獲取頁面源代碼,所有的 # 使用BS進行分析 soup = BeautifulSoup(html, 'lxml' ) items = soup.select( '#mainsrp-itemlist .items .item' ) for item in items: image = item.select( '.pic .img' )[ 0 ][ 'data-src' ] price = item.select( '.price strong' )[ 0 ].text deal = item.select( '.deal-cnt' )[ 0 ].text[: - 3 ] title = item.select( '.title' )[ 0 ].text.strip() shop = item.select( '.shop' )[ 0 ].text.strip() location = item.select( '.location' )[ 0 ].text product = { 'image' : image, 'price' : price, 'deal' : deal, 'title' : title, 'shop' : shop, 'location' : location } save_text(product) # 下載內容 def save_text(product): # 保存為txt文件,a追加寫模式,編碼模式utf-8 with open ( 'text.txt' , 'a' , encoding = 'utf-8' ) as f: # 使用JSON把字典轉換為str格式,加換行符 f.write(json.dumps(product, ensure_ascii = False ) + '\n' ) f.close() def main(): # 通過關鍵字在淘寶進行搜索 total = search( '美食' ) # 用正則提取頁碼數字 total = int (re. compile ( '(\d+)' ).search(total).group( 1 )) # 翻頁 for i in range ( 2 , total + 1 ): # 循環包含前,不包含尾 next_page(i) browser.close() if __name__ = = '__main__' : main() |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/wenboyu/article/details/78176859