【问题标题】:Python accessing specific elements on Menards websitePython 访问 Menards 网站上的特定元素
【发布时间】:2021-12-26 06:24:11
【问题描述】:

我目前正在尝试创建一个 Python 脚本,该脚本可以进入 Menards 网站并获取他们所有的产品描述、SKU 和价格,然后将它们导入到 Excel 文件中。所以我可以创建数据透视表或用他们的产品做其他事情。目前,我可以通过使用 XPath 等使其工作。但是这种方法不是很健壮,因为它依赖于每个不同网页的特定路径。我想做的是制作更强大的东西,可以在我正在查看的网站上使用。 这些是我指的网站。

https://www.menards.com/main/electrical/conduit-conduit-fittings-raceways/conduit/c-6423.htm?queryType=allItems&rid=ipKkIrbnch&shippingOptions_facet=Pickup+at+Store+Eligible'
https://www.menards.com/main/electrical/conduit-conduit-fittings-raceways/conduit-fittings-supports/c-9538.htm queryType=allItems&shippingOptions_facet=Pickup+at+Store+Eligible&sortby=priceAsc'
https://www.menards.com/main/electrical/light-switches-dimmers-outlets/light-switches/c-6324.htm?queryType=allItems&Spec_Color%2FFinishFamily_facet=Ivory&Spec_Color%2FFinishFamily_facet=Light+Almond&Spec_Color%2FFinishFamily_facet=White&shippingOptions_facet=Pickup+at+Store+Eligible&sortby=priceAsc/'

IRW = https://www.menards.com/main/electrical/electrical-wire-cable/indoor-electrical-cable/c-6441.htm?queryType=allItems&rid=HA1P3WveKh&shippingOptions_facet=Pickup+at+Store+Eligible

这是我目前在尝试找出更健壮的设计时所拥有的代码。

driver.get(website.get('IRW'))
driver.minimize_window()
description = driver.find_elements(By.CSS_SELECTOR, ".search-item")
print(len(description))
for ll in range(0, 1):
    print(description[ll].text)

目前,此代码仅通过其类名“search-items”定位网页上的所有元素。它定位了大多数元素等,但是我得到了很多垃圾,得到了我想要的结果。 这是我输出的代码示例:

> 11 Click here to go to detail page Bestseller 12 Variations Available
> 12 Gauge NM-B Cable with Ground Wire Click to add item "12 Gauge NM-B
> Cable with Ground Wire" to the compare list Compare Click to add item
> "12 Gauge NM-B Cable with Ground Wire" to the compare list Add To List
> Click to add item 12 Gauge NM-B Cable with Ground Wire to your list
> Sku # 3691660 $25.79 You Save $3.19 with Mail-In Rebate More
> Information Shipping CHOOSE VARIATION None

我想避免必须替换当前字符串并执行大量循环来获取值,因为所有这些垃圾在每个网页上都不一致。我希望这是定位元素的更好方法。任何想法谢谢!

【问题讨论】:

    标签: javascript python html selenium web-scraping


    【解决方案1】:

    您可以使用 css child > 和 descendant 组合子来指定具有 search-item 类的父级的直接子级或后代(分别),以仅隔离感兴趣的 href 的顺序

    links = [i.text for i in 
             driver.find_elements_by_css_selector("#search-items > .search-item .details > a")]
    

    【讨论】:

    • 这对我如何访问其他类名之间有空格的类很有帮助?
    • 这对于我如何访问其他类名之间有空格的类非常有帮助?目前正在吐槽:点击这里进入详细页面 12 可用的变体 14 号 NM-B 电缆,带地线,我真的只是想要产品的描述。
    • 如果有空格则替换为.
    【解决方案2】:

    谢谢 QHarr 我能够弄清楚如何使用这个特定的类访问其他类。允许我这样做的代码行如下:

    #for description
    description = [i.text for i in driver.find_elements(By.CSS_SELECTOR, "#search-items > .search-item .details > a > .row.pt-5.pb-sm-5 > .multilines-3.text-truncate-multilines.xs-single-col-8.col-12 > .font-weight-bold.text-dark")]
    print(description)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-16
      • 1970-01-01
      • 2015-06-07
      • 1970-01-01
      相关资源
      最近更新 更多