为了账号安全,请及时绑定邮箱和手机立即绑定

For 循环每个链接打印 3 个 -Python/BeautifulSoup

For 循环每个链接打印 3 个 -Python/BeautifulSoup

茅侃侃 2023-10-26 16:34:34
我有一个简单的 for 循环,它运行但打印每个链接 3 次。为什么?(页面共有 3 个“事件”)我试图从中抓取的网站 = https://www.aacr.org/get-involved/events/这是代码->import requestsfrom bs4 import BeautifulSoupimport pandas as pdproductlinks = []url='https://www.aacr.org/get-involved/events/'r=requests.get(url)soup=BeautifulSoup(r.content,'html.parser')productlist=soup.find_all('article',class_='event')for item in productlist:    for link in item.find_all('a',href=True):        productlinks.append(link['href'])print(productlinks)Any insights on how i can make it run 1 per link
查看完整描述

1 回答

?
肥皂起泡泡

TA贡献1829条经验 获得超6个赞

将最后一个 for 循环更改为:

for item in productlist:
    productlinks.append(item.a['href'])


查看完整回答
反对 回复 2023-10-26
  • 1 回答
  • 0 关注
  • 75 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信