试图从多个链接中抓取

这个脚本的目标是访问一个网站,生成特定产品的链接,然后从这些生成的链接中抓取。


在此脚本中,脚本将通过定义的属性获取来自产品主页上看到的四个特色产品的链接。链接保存在变量“links”中,其中包含四个特色产品的四个网址。


然后,我将使用请求来请求产品的每个 url 以使用 BeautifulSoup 抓取数据。


这是我的代码:


import time

from selenium import webdriver

import selenium.webdriver.chrome.service as service

import requests

from bs4 import BeautifulSoup



url = "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).text

soup = 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 ']'


繁花如伊
浏览 184回答 1
1回答

慕森王

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)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python