爬取華為手機資料(六)

2021-10-16 17:23:59 字數 3792 閱讀 4750

我發現這個工具對於爬取測試是不錯的,相比於pycharm而言,工具的選擇也是很重要的,合作共贏。

參考b站上的

學習效率更好一點。

(嘿嘿,在人家辛苦的創作獲取了知識,必須推廣一波)

**就不詳細解釋,多練很重要。

(我的**是在)

到達指定頁面,手動登入吧(我的**必須得掃碼登入)

from selenium import webdriver

url =

''#自己的安裝路徑,按你的修改一下

driver = webdriver.chrome(r'd:\py_project\selenium_demo\chromedriver.exe'

)driver.get(url)

`````python

pos = driver.find_elements_by_xpath(

'//div[@class="row row-2 title"]/a'

)len

(pos)

pos[0]

.click(

)#頁面的轉換

#爬取各個華為手機的名稱,**,月銷量,累計評價

因為要爬取的有44個,我為了簡單,就爬了兩部手機的資料。

driver.switch_to_window(driver.window_handles[0]

)for i in pos[:2

]:i.click(

) driver.switch_to_window(driver.window_handles[1]

) title = driver.find_element_by_xpath(

'//*[@id="j_detailmeta"]/div[1]/div[1]/div/div[1]/h1/a'

).text

print

(title)

price = driver.find_element_by_xpath(

'//*[@id="j_promoprice"]/dd/div/span'

).text

print

(price)

mcount = driver.find_element_by_xpath(

'//*[@id="j_detailmeta"]/div[1]/div[1]/div/ul/li[1]/div/span[2]'

).text

print

(mcount)

addcount = driver.find_element_by_xpath(

'//*[@id="j_itemrates"]/div/span[2]'

).text

print

(addcount)

print

("*************************==="

) driver.close(

) driver.switch_to_window(driver.window_handles[0]

)

優化**:

titles =

prices =

mcounts =

addcounts =

for i in pos[:2

]:i.click(

) driver.switch_to_window(driver.window_handles[1]

) title = driver.find_element_by_xpath(

'//*[@id="j_detailmeta"]/div[1]/div[1]/div/div[1]/h1/a'

).text

print

(title)

price = driver.find_element_by_xpath(

'//*[@id="j_promoprice"]/dd/div/span'

).text

print

(price)

mcount = driver.find_element_by_xpath(

'//*[@id="j_detailmeta"]/div[1]/div[1]/div/ul/li[1]/div/span[2]'

).text

print

(mcount)

addcount = driver.find_element_by_xpath(

'//*[@id="j_itemrates"]/div/span[2]'

).text

print

(addcount)

print

("*************************==="

) driver.close(

) driver.switch_to_window(driver.window_handles[0]

)

儲存資料,並以excel形式儲存

import pandas as pd

data = pd.dataframe(

)data[

'名稱'

]= titles

data[

'**'

]= prices

data[

'月銷量'

]= mcounts

data[

'累計評價'

]= addcounts

data

data.to_excel(

'華為手機資料.xlsx'

)

Scrapy Mongodb爬取資料

scrapy爬蟲資料存到mongodb中 其實主要是前兩步 1 在settings.py中進行配置 item pipelines mongodb host 127.0.0.1 mongodb port 27017 mongodb dbname spider1 mongodb docname book...

Python資料爬取

二.scrapy爬蟲框架 資料 網路資料採集模型 第三方庫實現爬取 pyspider爬蟲框架 scrapy爬蟲框架 安裝scrapy 配置scrapy環境變數 建立scrapy專案的命令 建立spider 使用python指令碼執行命令列啟動爬蟲 from scrapy.cmdline import...

用Python爬取京東手機評論

好評 中評 差評 追評 可以看到這個url裡都有以下幾個引數 videocount 2638,videocountstr 2600 aftercount 4852,aftercountstr 4800 showcount 24200,showcountstr 2.4萬 productid 10000...