【发布时间】: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/'
这是我目前在尝试找出更健壮的设计时所拥有的代码。
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