Python爬蟲爬取淘寶,京東商品信息



小編是一個理科生,不善長說一些廢話。簡單介紹下原理然后直接上代碼。

使用的工具(Python+pycharm2019.3+selenium+xpath+chromedriver)其中要使用pycharm也可以私聊我selenium是一個框架可以通過pip下載

pip install selenium -i https://pypi.tuna.tsinghua.edu.cn/simple/ 

(注)-i 后面是pip使用臨時清華源下載 比較快  使用pip原來的源很慢所以做了一個換源處理

chromedriver下載地址:http://chromedriver.storage.googleapis.com/index.html找到與自己的谷歌游覽器對應版本的版本版本對應關系這篇文章里面有:https://blog.csdn.net/BinGISer/article/details/88559532

京東流程。。。淘寶類似(就是多了一個登錄驗證)
PS:很多人在學習Python的過程中,往往因為遇問題解決不了或者沒好的教程從而導致自己放棄,為此我整理啦從基礎的python腳本到web開發、爬蟲、django、數據挖掘等【PDF等】需要的可以進Python全棧開發交流.裙 :一久武其而而流一思(數字的諧音)轉換下可以找到了,里面有最新Python教程項目可拿,不懂的問題有老司機解決哦,一起相互監督共同進步

一,要找到商場地址:https://www.jd.com/

二,模擬正常的查詢(正常查詢商品步驟:輸入商品名,點擊搜索,下拉查看商品,點擊下一頁查看更多的商品)怎么來的我不去做詳細的說明(懶得打字,能用就行,懶得去做文章教人,實在想學習加我扣扣討論)直接上代碼,能看懂就看,看不懂的可以加扣扣:2511217211一起討論(加好友驗證備注:討論)

 

爬取京東商品信息代碼:

from selenium import webdriver
from time import sleep
import re
import os


# 搜索商品
def search_products():
# 輸入商品名字
driver.find_element_by_xpath('//*[@id="key"]').send_keys(keyword)
# 點擊搜索
driver.find_element_by_xpath('//*[@class="form"]/button').click()
sleep(10)
token = driver.find_element_by_xpath('//*[@id="J_bottomPage"]/span[2]/em[1]/b').text
# 0代表所有匹配到的數字
token = int(re.compile('(\d+)').search(token).group(1))
# 返回總頁數
return token


# 下拉下滑條,加載數據
def drop_down():
for x in range(1, 11, 2):
sleep(1)
j = x / 10
js = 'document.documentElement.scrollTop = document.documentElement.scrollHeight * %f' % j
driver.execute_script(js)


# 獲取商品信息
def get_product():
lis = driver.find_elements_by_xpath('//*[@class="gl-warp clearfix"]/li[@class="gl-item"]')
for li in lis:
price = li.find_element_by_xpath('.//div[@class="p-price"]/strong/i').text + '元'
info = li.find_element_by_xpath('.//div[@class="p-name"]/a/em').text + li.find_element_by_xpath(
'.//div[@class="p-name"]/a').get_attribute('title')
p_commit = li.find_element_by_xpath('.//div[@class="p-commit"]/strong/a').text
p_shopnum = li.find_element_by_xpath('.//div[@class="p-shopnum"]/*').text
p_img = li.find_element_by_xpath('.//div[@class="p-img"]/a/img').get_attribute('src')
print(info, price, p_commit, p_shopnum, p_img, sep='|')


# 翻頁
def next_page():
token = search_products()
num = 1
while (num != token):
driver.get('https://search.jd.com/Search?keyword={}&page={}'.format(keyword, 2 * num - 1))
driver.implicitly_wait(10)
num += 1
drop_down()
get_product()


if __name__ == "__main__":
keyword = input('輸入你想查找的商品名字:')
driver_path = os.path.abspath(os.path.join(os.getcwd(), "..")) + "/Drive/chromedriver.exe"
driver = webdriver.Chrome(driver_path)
# 窗口最大化,防止數據丟失
driver.maximize_window()
driver.get('https://www.jd.com/')
next_page()
爬取淘寶信息的代碼from selenium import webdriverfrom time import sleep

import re
import os


# 搜索商品
def search_products():
driver.find_element_by_xpath('//*[@id="q"]').send_keys(keyword)
driver.find_element_by_xpath('//*[@id="J_TSearchForm"]/div[1]/button').click()
sleep(10)
token = driver.find_element_by_xpath('//*[@id="mainsrp-pager"]/div/div/div/div[1]').text
# 0代表所有匹配到的數字
token = int(re.compile('(\d+)').search(token).group(1))
return token


# 下拉下滑條,加載數據
def drop_down():
for x in range(1, 11, 2):
sleep(1)
j = x / 10
js = 'document.documentElement.scrollTop = document.documentElement.scrollHeight * %f' % j
driver.execute_script(js)


# 獲取商品信息
def get_product():
lis = driver.find_elements_by_xpath('//div[@class="items"]/div[@class="item J_MouserOnverReq "]')
for li in lis:
info = li.find_element_by_xpath('.//div[@class="row row-2 title"]').text
price = li.find_element_by_xpath('.//a[@class="J_ClickStat"]').get_attribute('trace-price') + '元'
deal = li.find_element_by_xpath('.//div[@class="deal-cnt"]').text
image = li.find_element_by_xpath('.//div[@class="pic"]/a/img').get_attribute('src')
name = li.find_element_by_xpath('.//div[@class="shop"]/a/span[2]').text
site = li.find_element_by_xpath('.//div[@class="location"]').text
print(info, price, deal, name, site, image, sep='|')


# 翻頁
def next_page():
token = search_products()
num = 0
while (num != token):
driver.get('https://s.taobao.com/search?q={}&s={}'.format(keyword, 44 * num))
driver.implicitly_wait(10)
num += 1
drop_down()
get_product()


if __name__ == "__main__":
keyword = input('輸入你想查找的商品名字:')
driver_path = os.path.abspath(os.path.join(os.getcwd(), ".."))+"/Drive/chromedriver.exe"
driver = webdriver.Chrome(driver_path)
# 窗口最大化,防止數據丟失
driver.maximize_window()
driver.get('https://www.taobao.com/')
next_page()
 
———
很多人在學習Python的過程中,往往因為遇問題解決不了或者沒好的教程從而導致自己放棄,為此我整理啦從基礎的python腳本到web開發、爬蟲、django、數據挖掘等【PDF等】需要的可以進Python全棧開發交流.裙 :一久武其而而流一思(數字的諧音)轉換下可以找到了,里面有最新Python教程項目可拿,不懂的問題有老司機解決哦,一起相互監督共同進步

本文的文字及圖片來源於網絡加上自己的想法,僅供學習、交流使用,不具有任何商業用途,版權歸原作者所有,如有問題請及時聯系我們以作處理。


免責聲明!

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



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