这个脚本的目标是访问一个网站,生成特定产品的链接,然后从这些生成的链接中抓取。在此脚本中,脚本将通过定义的属性获取来自产品主页上看到的四个特色产品的链接。链接保存在变量“links”中,其中包含四个特色产品的四个网址。然后,我将使用请求来请求产品的每个 url 以使用 BeautifulSoup 抓取数据。这是我的代码:import timefrom selenium import webdriverimport selenium.webdriver.chrome.service as serviceimport requestsfrom bs4 import BeautifulSoupurl = "https://www.vatainc.com/"service = service.Service('/Users/Name/Downloads/chromedriver.exe')service.start()capabilities = {'chrome.binary': '/Google/Chrome/Application/chrome.exe'}driver = webdriver.Remote(service.service_url, capabilities)driver.get(url)time.sleep(2)links = [x.get_attribute('href') for x in driver.find_elements_by_xpath("//*[contains(@class, 'product-name')]/a")]html = requests.get(links).textsoup = BeautifulSoup(html, "html.parser")products = soup.findAll("div")print(products)我得到的错误代码:未找到“[” https://www.vatainc.com/0240-bonnie-bone-marrow-biopsy-skills-trainertm.html ', ' https://www.vatainc.com/0910-seymour 的连接适配器-iitm-wound-care-model-1580.html ', ' https://www.vatainc.com/2410-chester-chestm-with-new-advanced-arm-1197.html ', ' https://www .vatainc.com/2365-advanced-four-vein-venipuncture-training-aidtm-dermalike-iitm-latex-free.html ']'
1 回答
慕森王
TA贡献1777条经验 获得超3个赞
links是字符串列表(URL)。您不能将 list 作为url参数传递给requests.get(). 尝试遍历此列表并逐个传递每个 URL 并获取每个页面:
for link in links:
html = requests.get(link).text
soup = BeautifulSoup(html, "html.parser")
products = soup.findAll("div")
print(products)
添加回答
举报
0/150
提交
取消