使用 Web Scraping 获取商品价格
Getting item price with Web Scraping
我想制作一个Python脚本,但不幸的是,当我想查看价格时,我得到的是NONE
而不是价格本身(如果我更改代码,则为 00.00 美元)。
我找到了很多例子,其中 HTML 与 id
一起使用,但我不知道如何使用 class
.
我目前拥有的:
import requests
from bs4 import BeautifulSoup
URL = 'https://www.banggood.com/BlitzWolf-BW-LT32-2M-USB-RGB-TV-Strip-Light-Kit-Sync-with-TV-Screen-Color-3-Sides-Cover-for-TV-Vivid-RGB-Color-Lighting-Effect-and-Dual-Simple-Control-p-1785284.html?akmClientCountry=HU&channel=facebookads&utm_source=facebookads&utm_medium=cpc&utm_campaign=app-dpa-all-ww-sm-afpur180-0407_prdshare_msg&utm_content=suki&utm_design=-&fbclid=IwAR3cAovrze4opttuwwpUxnXZ6dkRjhau3RqmDqASPW0mSEOI8sJgn9Cqss8&_branch_match_id=850392564428961100&cur_warehouse=CN&ID=45764'
headers = {"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537'}
page = requests.get(URL, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')
title = soup.find("span",{"class":'product-title-text'}).get_text()
price = soup.find("span",{"class":'main-price'}).get_text()
converted_price = price[0:5]
print(converted_price)
print(title)
当网站使用 Javascript 并动态变化时,BeautifulSoup
很少使用网络抓取。如今,大多数网站 Javascript 都很难抓取数据。替代选项之一是使用 Selenium
.
如果你已经用过Selenium
那么直接跳转到下面的代码块。如果没有,请按照以下说明进行操作。
- 在选项菜单(浏览器右上角)下的
About Chrome
中检查您使用的 Chrome 版本。
- 转到this网站并下载相同版本的驱动程序。
- 创建一个文件夹
C:\webdrivers
并将下载的驱动程序复制到该文件夹中。
- 复制文件路径
C:\webdrivers\chromedriver.exe
并将其添加到 environment variables
中的 PATH (
现在执行下面的代码:
from selenium import webdriver
opts = webdriver.ChromeOptions()
# The below line will make your browser run in background when uncommented
# opts.add_argument('--headless')
driver = webdriver.Chrome(options= opts, executable_path = 'C:\webdrivers\chromedriver.exe')
driver.get("https://www.banggood.in/BlitzWolf-BW-LT32-2M-USB-RGB-TV-Strip-Light-Kit-Sync-with-TV-Screen-Color-3-Sides-Cover-for-TV-Vivid-RGB-Color-Lighting-Effect-and-Dual-Simple-Control-p-1785284.html?akmClientCountry=HU&channel=facebookads&utm_source=facebookads&utm_medium=cpc&utm_campaign=app-dpa-all-ww-sm-afpur180-0407_prdshare_msg&utm_content=suki&utm_design=-&_branch_match_id=850392564428961100&cur_warehouse=CN&ID=45764")
prices = driver.find_elements_by_class_name('main-price')
for price in prices:
print(price.text)
driver.quit()
输出:
₹1,712.63
有关 Selenium
的更多详细信息,请查看 documentation,这是一个非常令人印象深刻的模块!
我想制作一个Python脚本,但不幸的是,当我想查看价格时,我得到的是NONE
而不是价格本身(如果我更改代码,则为 00.00 美元)。
我找到了很多例子,其中 HTML 与 id
一起使用,但我不知道如何使用 class
.
我目前拥有的:
import requests
from bs4 import BeautifulSoup
URL = 'https://www.banggood.com/BlitzWolf-BW-LT32-2M-USB-RGB-TV-Strip-Light-Kit-Sync-with-TV-Screen-Color-3-Sides-Cover-for-TV-Vivid-RGB-Color-Lighting-Effect-and-Dual-Simple-Control-p-1785284.html?akmClientCountry=HU&channel=facebookads&utm_source=facebookads&utm_medium=cpc&utm_campaign=app-dpa-all-ww-sm-afpur180-0407_prdshare_msg&utm_content=suki&utm_design=-&fbclid=IwAR3cAovrze4opttuwwpUxnXZ6dkRjhau3RqmDqASPW0mSEOI8sJgn9Cqss8&_branch_match_id=850392564428961100&cur_warehouse=CN&ID=45764'
headers = {"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537'}
page = requests.get(URL, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')
title = soup.find("span",{"class":'product-title-text'}).get_text()
price = soup.find("span",{"class":'main-price'}).get_text()
converted_price = price[0:5]
print(converted_price)
print(title)
BeautifulSoup
很少使用网络抓取。如今,大多数网站 Javascript 都很难抓取数据。替代选项之一是使用 Selenium
.
如果你已经用过Selenium
那么直接跳转到下面的代码块。如果没有,请按照以下说明进行操作。
- 在选项菜单(浏览器右上角)下的
About Chrome
中检查您使用的 Chrome 版本。 - 转到this网站并下载相同版本的驱动程序。
- 创建一个文件夹
C:\webdrivers
并将下载的驱动程序复制到该文件夹中。 - 复制文件路径
C:\webdrivers\chromedriver.exe
并将其添加到environment variables
中的 PATH (
现在执行下面的代码:
from selenium import webdriver
opts = webdriver.ChromeOptions()
# The below line will make your browser run in background when uncommented
# opts.add_argument('--headless')
driver = webdriver.Chrome(options= opts, executable_path = 'C:\webdrivers\chromedriver.exe')
driver.get("https://www.banggood.in/BlitzWolf-BW-LT32-2M-USB-RGB-TV-Strip-Light-Kit-Sync-with-TV-Screen-Color-3-Sides-Cover-for-TV-Vivid-RGB-Color-Lighting-Effect-and-Dual-Simple-Control-p-1785284.html?akmClientCountry=HU&channel=facebookads&utm_source=facebookads&utm_medium=cpc&utm_campaign=app-dpa-all-ww-sm-afpur180-0407_prdshare_msg&utm_content=suki&utm_design=-&_branch_match_id=850392564428961100&cur_warehouse=CN&ID=45764")
prices = driver.find_elements_by_class_name('main-price')
for price in prices:
print(price.text)
driver.quit()
输出:
₹1,712.63
有关 Selenium
的更多详细信息,请查看 documentation,这是一个非常令人印象深刻的模块!