【发布时间】:2020-05-27 17:56:26
【问题描述】:
我正在尝试通过使刮刀刮取页面上每个项目的链接来刮取网站搜索页面,然后再次解析刮取的链接并从该链接中刮取某些信息。我遇到的问题是,如果初始链接已被抓取,我想遍历每个链接并从中抓取数据,但它目前只抓取链接列表中的第一个值。我想问题出在我的 for 循环上。
这是我当前的代码。
productLinks = []
carMake = []
url = "https://buy.cars45.com/cars"
headers = {"Accept-Language": "en-US, en;q=0.5"}
searchResults = requests.get(url, headers=headers)
soup = BeautifulSoup(searchResults.text, "html.parser")
searchlinks = soup.find_all('div', class_='product_box')
for i in searchlinks:
a = i.find('a').get('href')
if a:
productLinks.append(a)
# print(a)
else:
productLinks.append('kNone')
for j in productLinks:
productPage = requests.get(j, headers=headers)
soup2 = BeautifulSoup(productPage.text, "html.parser")
details = soup2.find_all('span', class_='float-right')
make = details[0].text
carName.append(make)
但它只是返回重复多次的第一辆车作为输出。
【问题讨论】:
标签: python html web-scraping beautifulsoup