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

从 HTML 标签 Python/BeautifulSoup 获取第二个元素

从 HTML 标签 Python/BeautifulSoup 获取第二个元素

幕布斯7119047 2023-10-26 10:51:22
我想从页面中抓取元素,例如页面 - https://www.aacr.org/?s=breast+cancer&search_type=global标题的 html 标签包含一个 html 链接和附加的标题。当我运行代码时,它会打印 HTML(第一个位置),然后打印标题(第二个位置/我想要的内容)例如 - 打印返回 -> <a href="https://www.aacr.org/ Patients-caregivers/cancer/breast-cancer/" title="Breast Cancer ">乳腺癌,我只想要粗体/第二个元素,有什么帮助吗?这是我的代码 -import requestsimport timefrom bs4 import BeautifulSoupimport pandas as pdproductlinks = []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')        print(title)
查看完整描述

2 回答

?
阿晨1998

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



查看完整回答
反对 回复 2023-10-26
?
三国纷争

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


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

添加回答

举报

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