日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python爬虫实例电商_价值上千元的Python爬虫外包案例,学会你就赚了

發布時間:2025/4/5 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python爬虫实例电商_价值上千元的Python爬虫外包案例,学会你就赚了 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

隨著互聯網時代的到來,人們更加傾向于互聯網購物。某寶又是電商行業的巨頭,在某寶平臺中有很多商家數據。

今天帶大家使用python+selenium工具獲取這些公開的

適合人群:

Python零基礎、對爬蟲數據采集感興趣的同學!

環境介紹:

python 3.6

pycharm

selenium

time

1、安裝selenium模塊

pip install selenium

2、請求網頁地址

if __name__ == '__main__':

keyword = input('請輸入你要查詢的商品數據:')

driver = webdriver.Chrome()

driver.get('https://www.taobao.com')

main()

def search_product(key):

"""模擬搜索商品,獲取最大頁數"""

driver.find_element_by_id('q').send_keys(key) ?# 根據id值找到搜索框輸入關鍵字

driver.find_element_by_class_name('btn-search').click() ?# 點擊搜索案例

driver.maximize_window() ?# 最大化窗口

time.sleep(15)

page = driver.find_element_by_xpath('//*[@id="mainsrp-pager"]/div/div/div/div[1]') ?# 獲取頁數的標簽

page = page.text ?# 提取標簽的文字

page = re.findall('(\d+)', page)[0]

# print(page)

return int(page)

4、獲取商品數據

def get_product():

divs = driver.find_elements_by_xpath('//div[@class="items"]/div[@class="item J_MouserOnverReq ?"]')

for div in divs:

info = div.find_element_by_xpath('.//div[@class="row row-2 title"]/a').text ?# 商品名稱

price = div.find_element_by_xpath('.//strong').text + '元' ?# 商品價格

deal = div.find_element_by_xpath('.//div[@class="deal-cnt"]').text ?# 付款人數

name = div.find_element_by_xpath('.//div[@class="shop"]/a').text ?# 店鋪名稱

print(info, price, deal, name, sep='|')

with open('data.csv', 'a', newline='') as csvfile: ?# newline='' ?指定一行一行寫入

csvwriter = csv.writer(csvfile, delimiter=',') ?# delimiter=',' ?csv數據的分隔符

csvwriter.writerow([info, price, deal, name]) ?# 序列化數據,寫入csv

def main():

search_product(keyword)

page = get_product()

完整代碼如下:

from selenium import webdriver

import time

import re

import csv

def search_product(key):

"""模擬搜索商品,獲取最大頁數"""

driver.find_element_by_id('q').send_keys(key)? # 根據id值找到搜索框輸入關鍵字

driver.find_element_by_class_name('btn-search').click()? # 點擊搜索案例

driver.maximize_window()? # 最大化窗口

time.sleep(15)

page = driver.find_element_by_xpath('//*[@id="mainsrp-pager"]/div/div/div/div[1]')? # 獲取頁數的標簽

page = page.text? # 提取標簽的文字

page = re.findall('(\d+)', page)[0]

# print(page)

return int(page)

def get_product():

divs = driver.find_elements_by_xpath('//div[@class="items"]/div[@class="item J_MouserOnverReq? "]')

for div in divs:

info = div.find_element_by_xpath('.//div[@class="row row-2 title"]/a').text? # 商品名稱

price = div.find_element_by_xpath('.//strong').text + '元'? # 商品價格

deal = div.find_element_by_xpath('.//div[@class="deal-cnt"]').text? # 付款人數

name = div.find_element_by_xpath('.//div[@class="shop"]/a').text? # 店鋪名稱

print(info, price, deal, name, sep='|')

with open('data.csv', 'a', newline='') as csvfile:? # newline=''? 指定一行一行寫入

csvwriter = csv.writer(csvfile, delimiter=',')? # delimiter=','? csv數據的分隔符

csvwriter.writerow([info, price, deal, name])? # 序列化數據,寫入csv

def main():

search_product(keyword)

page = get_product()

if __name__ == '__main__':

keyword = input('請輸入你要查詢的商品數據:')

driver = webdriver.Chrome()

driver.get('https://www.taobao.com')

main()

喜歡的就請關注加點贊

總結

以上是生活随笔為你收集整理的python爬虫实例电商_价值上千元的Python爬虫外包案例,学会你就赚了的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。