簡體   English   中英

使用 BeautifulSoup 和 Selenium 的抓取問題

[英]Scraping issue using BeautifulSoup and Selenium

我開始為自己編碼,但我在代碼行上被阻止了。 你能給我一些解釋嗎?

我想從這個 div 標簽中抓取信息:

role = experience1_div('span', {'class' : 'mr1 t-bold'}) print(role)

輸出 :

[<span class="mr1 t-bold"> <span aria-hidden="true"><!-- -->Automation Engineer - Intern<!-- --></span><span class="visually-hidden"><!-- -->Automation Engineer - Intern<!-- --></span> </span>]

我怎樣才能只獲得 HTML 文本:“自動化工程師 - 實習生”

我試過這個函數.get_text().strip()但似乎span標簽阻止了我的函數....

我不知道experience1_div是什么,但要獲取所有文本使用role.text

role = experience1_div.find('span', {'class' : 'mr1 t-bold'}) 
print(role.text)

輸出: Automation Engineer - InternAutomation Engineer - Intern

要從第一個嵌套跨度中獲取文本,請使用role.span.text

或從第二個嵌套跨度role.contents[2].text

提供的信息中的主要問題是您已經生成了一個ResultSet - 要獲取其文本,您必須直接選擇元素或對其進行迭代。

role[0].span.get_text(strip=True)

或者

for e in role:
    print(e.span.get_text(strip=True))

輸出:

Automation Engineer - Intern

更好的方法是選擇更具體的元素(根據您的示例):

experience1_div.select_one('span.mr1.t-bold > span').get_text(strip=True)

這是實現目標的最簡單技術。

 role = experience1_div.select_one('span.mr1.t-bold >span').get_text(strip=True) print(role)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM