【问题标题】:Inspecting element is different from what is returned by BeautifulSoup/Selenium检查元素与 BeautifulSoup/Selenium 返回的元素不同
【发布时间】:2018-07-09 18:50:16
【问题描述】:

我正在尝试为多个网站制作价格抓取工具,但我遇到了一个特定网站的问题。检查价格时,小数点显示“56”,但当我使用 BeautifulSoup 下载 HTML 时,它返回 16。其他产品也会出现同样的问题。

到目前为止,您可以在下面看到我的代码:

from bs4 import BeautifulSoup
import requests

myProxy = {"http"  : "http://10.120.118.49:8080", "https"  : 
"https://10.120.118.49:8080"}

url = 'https://shop.rewe.de/coca-cola-4x1-5l/PD6731201'

page = requests.get(url, proxies = myProxy)
soup = BeautifulSoup(page.text, 'html.parser')

predecimal = soup.find('span', attrs={'class': 'pd-price__predecimal'})
predec = predecimal.text.strip()

separator = soup.find('span', attrs={'class': 'pd-price__separator'})
sep = separator.text.strip()

decimal = soup.find('span', attrs={'class': 'pd-price__decimal'})
dec = decimal.text.strip()

price = str(predec) + str(sep) + str(dec)

print(price)

上面的代码返回 5,16,而网站上显示的价格是 5,56。对于其他一些网站,我成功地使用了 Selenium,但在这种情况下,它仍然返回相同的数字。任何帮助将不胜感激!

【问题讨论】:

  • 5,56 是否可能只是在您的浏览器缓存中,因为他们今天才开始销售或其他什么?
  • 我不这么认为,因为价格已经有一段时间了。它还在不同的设备上显示相同的价格。

标签: python python-3.x selenium web-scraping beautifulsoup


【解决方案1】:

如前所述,我也尝试过使用 Selenium,使用以下代码:

driver.get('https://shop.rewe.de/coca-cola-4x1-5l/PD6731201')
xpath = '//*[@class="pd-PriceInformation"]/mark'
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, xpath)))
price = driver.find_elements_by_xpath(xpath)
for p in price:
    print(p.text)

这将返回“ab 5, 16 €”,因此与使用 BeautifulSoup 时基本相同。当为网站https://shop.rewe.de/vio-bio-limo-orange-4x1l/PD2455462 尝试相同的代码时,它会返回“ab 5, 96 €”,这也是错误的,因为在这种情况下价格也是 5,56。

【讨论】:

    猜你喜欢
    • 2020-07-31
    • 2020-10-13
    • 2019-02-26
    • 2020-04-13
    • 1970-01-01
    • 1970-01-01
    • 2020-07-27
    • 2010-10-17
    • 1970-01-01
    相关资源
    最近更新 更多