第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

從 HTML 標(biāo)簽 Python/BeautifulSoup 獲取第二個(gè)元素

從 HTML 標(biāo)簽 Python/BeautifulSoup 獲取第二個(gè)元素

幕布斯7119047 2023-10-26 10:51:22
我想從頁面中抓取元素,例如頁面 - https://www.aacr.org/?s=breast+cancer&search_type=global標(biāo)題的 html 標(biāo)簽包含一個(gè) html 鏈接和附加的標(biāo)題。當(dāng)我運(yùn)行代碼時(shí),它會(huì)打印 HTML(第一個(gè)位置),然后打印標(biāo)題(第二個(gè)位置/我想要的內(nèi)容)例如 - 打印返回 -> <a href="https://www.aacr.org/ Patients-caregivers/cancer/breast-cancer/" title="Breast Cancer ">乳腺癌,我只想要粗體/第二個(gè)元素,有什么幫助嗎?這是我的代碼 -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貢獻(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



查看完整回答
反對 回復(fù) 2023-10-26
?
三國紛爭

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


查看完整回答
反對 回復(fù) 2023-10-26
  • 2 回答
  • 0 關(guān)注
  • 292 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號