【问题标题】:Scraping lowes.com using Selenium and BeautifulSoup Price Issue使用 Selenium 和 BeautifulSoup 价格问题抓取 lowes.com
【发布时间】:2021-07-28 01:00:13
【问题描述】:

我正在尝试抓取 lowes.com 产品详细信息,这是我正在尝试运行的脚本

from bs4 import BeautifulSoup
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_experimental_option('prefs', {
    'geolocation': True
})

#driver = webdriver.Chrome(ChromeDriverManager().install(),options=chrome_options)
#driver.execute_cdp_cmd("Page.setGeolocationOverride", {
#    "latitude": 34.052235,
#    "longitude": -118.243683,
#    "accuracy": 98
#})
driver.get("https://www.lowes.com/pd/Therma-Tru-Benchmark-Doors-Craftsman-Simulated-Divided-Light-Right-Hand-Inswing-Ready-To-Paint-Fiberglass-Prehung-Entry-Door-with-Insulating-Core-Common-36-in-x-80-in-Actual-37-5-in-x-81-5-in/1000157897")
driver.execute_script("window.scrollTo(0,document.body.scrollHeight/5)")
time.sleep(1)
driver.execute_script("window.scrollTo(0,(document.body.scrollHeight/5)*2)")
time.sleep(1)
driver.execute_script("window.scrollTo(0,(document.body.scrollHeight/5)*3)")
time.sleep(1)
driver.execute_script("window.scrollTo(0,(document.body.scrollHeight/5)*4)")
time.sleep(1)
driver.execute_script("window.scrollTo(0,(document.body.scrollHeight/5)*5)")
time.sleep(1)
content = driver.page_source
soup = BeautifulSoup(content,'html.parser')
imgs = soup.findAll("img", attrs={"class":"met-epc-item"})
for img in imgs:
    print(img.get("src"))
print("Price: "+soup.find("span", attrs={"class":"aPrice large"}).text)
brand = soup.find("a", attrs={"class":"Link__LinkStyled-RC__sc-b3hjw8-0 bYfcYt"})
print("brand url: "+ brand.get("href"))
print("brand name: "+ brand.get("text"))
print("brand desc: "+soup.find("h1", attrs={"class":"style__HeaderStyle-PDP__y7vp5g-12 iMECxW"}).text)
driver.close()

当我尝试执行此脚本时,价格元素会导致该元素不存在的错误,当我查看使用 selenium 打开的 chrome 实例中的页面时,我发现价格没有出现,并且一个文本框要求输入邮政编码或城市或州以显示价格和可用性,当尝试输入任何邮政编码或城市或州时没有发生任何事情,当我尝试刷新或在lowes网站中输入任何其他URL时,它说访问被拒绝并重新输入lowes需要的任何其他URL使用 selemnium 打开新的 chrome 实例。有什么建议可以解决这个问题并正确刮掉产品吗?我还想说明一下,当我从我的普通浏览器 chrome 打开网站时,它会正确打开,显示价格,而不是像我们一样拒绝任何访问

【问题讨论】:

    标签: python selenium web-scraping beautifulsoup


    【解决方案1】:

    数据可通过发送GET请求到:

    https://www.lowes.com/pd/1000157897/productdetail/1674/Guest
    

    您可以尝试此解决方案以在不使用 Selenium 的情况下获取数据。 (类似于@Andrej Kesely 的answer,但这里的 URL 不同)。

    import requests
    
    
    id_ = "1000157897"
    url = "https://www.lowes.com/pd/{}/productdetail/1674/Guest".format(id_)
    headers = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36"
    }
    
    response = requests.get(url, headers=headers).json()
    
    print("Product price: $", response["productDetails"][_id]["price"]["itemPrice"] )
    print("Product price:", response["productDetails"][_id]["product"]["brand"])
    print("Product description:", response["productDetails"][_id]["product"]["description"])
    

    输出:

    Product price: $ 370
    Product price: Therma-Tru Benchmark Doors
    Product description: 36-in x 80-in Fiberglass Craftsman Right-Hand Inswing Ready to paint Unfinished Prehung Single Front Door with Brickmould
    

    【讨论】:

    • 好的,谢谢你成功了,但我想问一下。我可以执行的请求数量是否有限制,以便我的 IP 地址不会被阻止?因为我想抓取数百甚至数千种产品
    • @SarahEldawody 我不确定。但是由于我们添加了headers,它应该有助于不被阻止。
    【解决方案2】:

    您要查找的数据以 Json 格式嵌入页面中:

    import re
    import json
    import requests
    
    headers = {
        "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:88.0) Gecko/20100101 Firefox/88.0"
    }
    
    url = "https://www.lowes.com/pd/Therma-Tru-Benchmark-Doors-Craftsman-Simulated-Divided-Light-Right-Hand-Inswing-Ready-To-Paint-Fiberglass-Prehung-Entry-Door-with-Insulating-Core-Common-36-in-x-80-in-Actual-37-5-in-x-81-5-in/1000157897"
    t = requests.get(url, headers=headers).text
    
    data = re.search(r"window\['__PRELOADED_STATE__'\] = (\{.*?\})<", t)
    data = json.loads(data.group(1))
    
    # uncomment to print all data:
    # print(json.dumps(data, indent=4))
    
    item_id = url.split("/")[-1]
    
    print("Name:", data["productDetails"][item_id]["product"]["brand"])
    print("Desc:", data["productDetails"][item_id]["product"]["description"])
    print("Price:", data["productDetails"][item_id]["price"]["itemPrice"])
    

    打印:

    Name: Therma-Tru Benchmark Doors
    Desc: 36-in x 80-in Fiberglass Craftsman Right-Hand Inswing Ready to paint Unfinished Prehung Single Front Door with Brickmould
    Price: 370
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多