这里的网页抓取初学者。我正在尝试获取此网页上的项目数:https://www.asos.com/dk/maend/a-to-z-of-brands/nike/cat/?cid=4766&refine=attribute_10992:61388&nlid=mw|sko|shop+efter+brand
但是,当我使用len()函数时,它表示语法有错误。
from bs4 import BeautifulSoup
import requests
import selenium
from selenium.webdriver import Firefox
driver = Firefox()
url = "https://www.asos.com/dk/maend/a-to-z-of-brands/nike/cat/?cid=4766&refine=attribute_10992:61388&nlid=mw|sko|shop+efter+brand"
driver.get(url)
items = len(driver.find_elements_by_xpath(//*[@id="product-12257648"])
for item in range(items):
price = item.find_element_by_xpath("/html/body/main/div/div/div/div[2]/div/div[1]/section/div/article[16]/a/p/span[1]")
print(price)
然后输出此错误:
File "C:/Users/rasmu/PycharmProjects/du nu ffs/jsscrape.py", line 13
items = len(driver.find_elements_by_xpath(//*[@id="product-12257648"])
^
SyntaxError: invalid syntax
Process finished with exit code 1
尝试一下
item = len(driver.find_elements_by_xpath(“ // * [@ id ='product-12257648']”))]
您需要在xpath中使用双引号
但是如果您要所有价格试试这个代码
从硒导入网络驱动程序
driver = webdriver.Firefox()
url =“ https://www.asos.com/dk/maend/a-to-z-of-brands/nike/cat/?cid=4766&refine=attribute_10992:61388&nlid=mw|sko|shop+efter+brand”driver.get(URL)
用于driver.find_elements_by_xpath(“ // p [./ span [@ data-auto-id ='productTilePrice']]”)):打印(item.text)
driver.close()