2 回答

TA貢獻(xiàn)2037條經(jīng)驗(yàn) 獲得超6個(gè)贊
你必須再做一次迭代才能得到你想要的,通過迭代每個(gè) a 標(biāo)簽(我保持你的代碼完整并添加了額外的循環(huán),這樣你就可以看到一般如何做到這一點(diǎn)的具體細(xì)節(jié),而不僅僅是對于這個(gè)特定的用例)。
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'])
結(jié)果:
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貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超7個(gè)贊
要獲取該title屬性,只需將最后一個(gè)更改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
添加回答
舉報(bào)