【问题标题】:Beautiful Soup/ Selenium web scraping美丽的汤/硒网页抓取
【发布时间】:2021-03-30 11:40:02
【问题描述】:

我正在尝试从一个本地网站获取产品名称及其价格。

网站是动态加载的,因此 requests 不支持它。我正在使用硒和美丽的汤。

但是它会重复计算每个产品(我为同一产品获得 2 个链接),有什么解决方案吗?

此外,在获取产品链接后,我需要获取产品信息(例如,名称和价格),但它再次计算产品并且不获取名称和价格。

我的代码:

import pandas as pd        
from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
    
productlinks = []
baseurl = "https://www.technodom.kz/"
options = Options()
options.headless = True

driver = webdriver.Chrome(r"C:\path\to\chromedriver.exe", options=options)


for x in range(1, 5):
    driver.get(
        f"https://www.technodom.kz/bytovaja-tehnika/uhod-za-odezhdoj/stiral-nye-mashiny/f/brands/lg/brands/samsung?page={x}"
    )
    # Wait for the page to fully render
    sleep(3)
    soup = BeautifulSoup(driver.page_source, "lxml")
    product_list = soup.find_all("li", class_="ProductCard")
    for item in product_list:
        for link in item.find_all("a", href=True):
            productlinks.append(baseurl + link["href"])
    print(productlinks)

wmlist = []
for link in productlinks:
    driver.get(link)
    soup = BeautifulSoup(driver.page_source, "lxml")
    print(link)
    name = soup.find('h1', class_='ProductHeader-Title').text.strip()
    price = soup.find('p', class_='ProductPrice ProductInformation-Price').text.strip()

    wm = {
        'Model':name,
        'Price': price
    }
    wmlist.append(wm)
    print('Saving:', wm['Model'])
df = pd.DataFrame(wmlist)

df.to_excel("TD pricesTEST.xlsx", sheet_name='TEW', index=False)

【问题讨论】:

  • productlinks 未定义,您正在使用 pandas 但没有导入。
  • productlinks 可能应该是 product_links。 item.find_all("a", href=True) 也会多次返回相同的 URL,因为它是 HTML!您可以定位正确的标签或在追加之前使用 IF 语句检查 url 是否唯一。
  • 其实productlinks已经定义好了,我导入了pandas,在代码中添加了。

标签: python selenium selenium-webdriver web-scraping beautifulsoup


【解决方案1】:

那些嵌套循环应该被归咎于你的输出加倍。此外,您只需要一个<a> 标记,其类为ProductCard-Content

我已经稍微简化了您的代码,您可以通过以下方式获取产品名称、价格和链接,最后将它们转储到 Excel 文件中:

from time import sleep

import pandas as pd
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.headless = True
driver = webdriver.Chrome(options=options)

final_output = []
pages = list(range(1, 5))

for page_number in pages:
    print(f"Scraping page: {page_number} / {len(pages)}")
    driver.get(
        f"https://www.technodom.kz/bytovaja-tehnika/uhod-za-odezhdoj/"
        f"stiral-nye-mashiny/f/brands/lg/brands/samsung?page={page_number}"
    )
    sleep(5)
    soup = BeautifulSoup(
        driver.page_source,
        "lxml",
    ).find_all("a", class_="ProductCard-Content")

    links = [f"https://www.technodom.kz/{anchor['href']}" for anchor in soup]
    names = [name.find("h4").getText() for name in soup]
    prices = [price.find("data")["value"] for price in soup]

    final_output.append(
        [
            [name, price, link] for name, price, link
            in zip(names, prices, links)
        ]
    )

df = pd.DataFrame(
    [data for sub_list in final_output for data in sub_list],
    columns=["NAME", "PRICE", "LINK"],
)
df.to_excel("test.xlsx", sheet_name='TEW', index=False)

输出:

【讨论】:

  • 您好,感谢您的评论,但是我注意到代码只下载了第一页,24 个模型。
  • 仍然没有得到所有数据,excel表中的产品总数是49,但在网站上是85
  • 渲染页面的时间似乎不够,所以我将睡眠时间增加到 5 秒,现在它可以工作了。
  • 好吧,我无法重现这一点,因为我得到了一个 85 的 Excel 文件(包括标题)。确保您在此处 range(1, 5) 具有正确的值并让脚本完整运行。
  • 如果它起作用了,请考虑投票和/或接受答案。
猜你喜欢
  • 1970-01-01
  • 2019-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-13
  • 2021-12-06
  • 2021-08-13
  • 1970-01-01
相关资源
最近更新 更多