2 回答
TA贡献2037条经验 获得超6个赞
你必须再做一次迭代才能得到你想要的,通过迭代每个 a 标签(我保持你的代码完整并添加了额外的循环,这样你就可以看到一般如何做到这一点的具体细节,而不仅仅是对于这个特定的用例)。
import requests
import time
from bs4 import BeautifulSoup
import pandas as pd
productlinks = []
sam=[]
for x in range(1,3):
url=f'https://www.aacr.org/page/{x}/?s=breast+cancer&search_type=global'
r=requests.get(url)
soup=BeautifulSoup(r.content,'html.parser')
productlist=soup.find_all('div',class_='blog-content')
for item in productlist:
title=soup.find_all('h3')
for single in title:
print(single.a['title'])
结果:
Breast Cancer
Male Breast Cancer
Breast Cancer Prevention (PDQ®)
Breast Cancer Screening (PDQ®)
Breast Cancer Treatment During Pregnancy (PDQ®)
Breast Cancer Treatment (PDQ®)
Male Breast Cancer Treatment (PDQ®)
Carcinoma of Unknown Primary
Overcoming Triple-Negative Breast Cancer
Living with Metastatic Breast Cancer
Surviving Metastatic Breast Cancer; Advocating for Other Cancer Patients
Living With Stage 4 Breast Cancer
Choosing to Enjoy Life Despite Metastatic Breast Cancer
A Breast and Colon Cancer Survivor Supports Cancer Research
Pedaling for Cancer Research
Emily Garnett
Supporting Increased Funding for Clinical Trials
Raising Awareness of Male Breast Cancer
Keeping Breast Cancer at Bay with Immunotherapy
Recovering after Breast Cancer Treatment Thanks to Prehab and Rehab
Takae Brewer, MD
Thankful for Clinical Trials
Bianca Lundien Kennedy
Gina Favors
Running to Beat Leukemia (and All Cancers)
Patricia Fox
Survivor Profile: An Unlikely Pivot
Program
Advances in Breast Cancer Research
Program
Breast Cancer
Male Breast Cancer
Breast Cancer Prevention (PDQ®)
Breast Cancer Screening (PDQ®)
Breast Cancer Treatment During Pregnancy (PDQ®)
Breast Cancer Treatment (PDQ®)
Male Breast Cancer Treatment (PDQ®)
Carcinoma of Unknown Primary
Overcoming Triple-Negative Breast Cancer
Living with Metastatic Breast Cancer
Surviving Metastatic Breast Cancer; Advocating for Other Cancer Patients
Living With Stage 4 Breast Cancer
Choosing to Enjoy Life Despite Metastatic Breast Cancer
A Breast and Colon Cancer Survivor Supports Cancer Research
Pedaling for Cancer Research
Emily Garnett
TA贡献1804条经验 获得超7个赞
要获取该title属性,只需将最后一个更改for loop为:
for item in productlist:
a_tag =item.find('a')
print(a_tag['title'])
输出:
Breast Cancer
Male Breast Cancer
Breast Cancer Prevention (PDQ®)
Breast Cancer Screening (PDQ®)
Breast Cancer Treatment During Pregnancy (PDQ®)
Breast Cancer Treatment (PDQ®)
Male Breast Cancer Treatment (PDQ®)
Carcinoma of Unknown Primary
Overcoming Triple-Negative Breast Cancer
Living with Metastatic Breast Cancer
Surviving Metastatic Breast Cancer; Advocating for Other Cancer Patients
Living With Stage 4 Breast Cancer
Choosing to Enjoy Life Despite Metastatic Breast Cancer
A Breast and Colon Cancer Survivor Supports Cancer Research
Pedaling for Cancer Research
Emily Garnett
Supporting Increased Funding for Clinical Trials
Raising Awareness of Male Breast Cancer
Keeping Breast Cancer at Bay with Immunotherapy
Recovering after Breast Cancer Treatment Thanks to Prehab and Rehab
Takae Brewer, MD
Thankful for Clinical Trials
Bianca Lundien Kennedy
Gina Favors
Running to Beat Leukemia (and All Cancers)
Patricia Fox
Survivor Profile: An Unlikely Pivot
Program
Advances in Breast Cancer Research
Program
添加回答
举报