python - 美丽的汤在一个网站上返回空列表,但在另一个网站上有效
问题描述
我目前正在通过“用 Python 自动化无聊的东西”来学习 Python。我现在正在做 Web Scraping 部分。
我编写了从一个网站获取产品价格的代码。但是,当我稍微编辑我的代码以在另一个网站上工作时似乎不起作用,并且 Beautiful Soup 从 CSS 返回一个空列表。
这是我的工作代码。
import bs4, requests, re
def getPrice(productUrl):
res = requests.get(productUrl)
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, 'html.parser')
# Go through CSS and get price
source = soup.select('#product_addtocart_form > div.product-shop > div.details-info')
element = source[0].text.strip()
# Regex for getting the price from the rest of the CSS.
pattern = re.compile(r"""R([1-9]\d*)(\.\d\d)?(?![\d.])""")
# Get price from string using regex pattern
trueprice = re.split(pattern, element)
return("The product's price is : R " + trueprice[1])
product = "https://www.faithful-to-nature.co.za/green-home-paper-straws-in-compostable-bag"
weblink = getPrice(product)
print(weblink)
这是我为另一个不起作用的网站编辑的代码。我注释掉了一些代码,因为当列表中没有数据时它没有任何功能。
import bs4, requests, re
def getPrice(productUrl):
res = requests.get(productUrl)
res.raise_for_status() # Check for any errors in request
soup = bs4.BeautifulSoup(res.text, 'html.parser')
# Go through CSS and get price
csssource = soup.select('#shopfront-app > div > div.grid-container.pdp-grid-container > div.grid-x.grid-margin-x > div > div > div > div > div.cell.medium-auto > div.pdp-core-module_actions_mdYzm > div.sf-buybox.pdp-core-module_buybox_q5wLs.buybox-module_buybox_eWK2S')
#element = csssource[0].text.strip()
# Regex for getting the price from the rest of the CSS.
pattern = re.compile(r"""R([1-9]\d*)(\.\d\d)?(?![\d.])""")
#trueprice = re.split(pattern, element)
#return("The product's price is : R " + trueprice[1])
print(csssource)
test1 = "https://www.takealot.com/lego-classic-basic-brick-set-11002/PLID53430493"
weblink = getPrice(test1)
print(weblink)
在这两个站点中,我使用 Chrome 上的检查方法获得了 CSS 选择器。我尝试使用更广泛的 CSS 选择器,但 Beautiful Soup 仍然返回一个空列表。
如何让 Beautiful Soup 返回正确的列表/CSS 选择器?
解决方案
嗨,我相信这个网站正在提供动态内容,所以你需要使用 selenium,当我尝试只用请求/bs 抓取时,我也会得到空白列表。您可能可以使用您原来的 css 选择标准,但我选择了您试图获得的价格的第 5 次出现的货币。
下载正确的壁虎驱动程序并在脚本中设置路径。
https://github.com/mozilla/geckodriver/releases
from bs4 import BeautifulSoup
from selenium import webdriver
import time
#self.driver = webdriver.Firefox(executable_path = 'D:\Selenium_RiponAlWasim\geckodriver-v0.18.0-win64\geckodriver.exe')
driver = webdriver.Firefox()
driver.get('https://www.takealot.com/lego-classic-basic-brick-set-11002/PLID53430493')
html = driver.page_source
soup = BeautifulSoup(html,'lxml')
i = 0
for span in soup.find_all('span',{'class' : 'currency'}):
if(i == 4):
print(span.text)
i += 1
#driver.close()
#returns R 315
推荐阅读
- php - 我想为 Android、IOS 以及 PHP 使用 Fastlane 工具自动化
- c# - 带字典的 ListView
- javascript - 为什么 input type=text 的 document.getElementById 失败
- jquery - 使用 jquery 输入类型文件的 base64 字符串
- asp.net-core-2.0 - 如何在 Asp.NET ZERO / Boilerplate 中迁移租户数据库和主机数据库?
- ruby-on-rails - Ruby on Rails 分离管理文件夹、控制器和布局
- javascript - 我可以在 Vue js 中同时使用 SASS 和 LESS
- c++ - g++/clang++ 反转编译错误顺序
- go - 任何人都可以理解 connStateInterface 吗?
- python-3.x - 如何使用聊天机器人的 API 将 sqlite3 转换为 csv 格式?