我正在嘗試使用硒和美麗的湯進行網路抓取,但我無法讓硒找到我需要的元素并回傳文本。
這是html:
<span class="t-14 t-normal">
<span aria-hidden="true"><!---->Crédit Agricole CIB · Full-time<!----></span><span class="visually-hidden"><!---->Crédit Agricole CIB · Full-time<!----></span>
</span>
您知道如何從此 html 中獲取文本“Crédit Agricole CIB Full-time”嗎?
我正在嘗試做這樣的事情:
src = driver.page_source
soup = BeautifulSoup(src, 'lxml') # Now using beautiful soup
intro = soup.find('div', {'class': 'pv-text-details__left-panel'})
text_loc = intro.find( ???? ) # Extracting the text
text = text_loc.get_text().strip() # Removing extra blank space
我不知道該放什么????
uj5u.com熱心網友回復:
如果不確切知道完整的 HTML 是什么樣子,我無法確認 - 在問題中共享的代碼段之前可能還有其他非常相似的嵌套元素,但如果沒有,那么您可以使用下面使用soup.select_one的css 選擇器:
spanTxt1 = soup.select_one('span.t-14.t-normal span[aria-hidden="true"]')
if spanTxt1 is not None: spanTxt1 = spanTxt1.get_text(strip=True)
spanTxt2 = soup.select_one('span.t-14.t-normal span.visually-hidden')
if spanTxt2 is not None: spanTxt2 = spanTxt2.get_text(strip=True)
print(f' Text1: "{spanTxt1}" \n Text2: "{spanTxt2}" ')
應該給出輸出
Text1: "Crédit Agricole CIB · Full-time"
Text2: "Crédit Agricole CIB · Full-time"
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/519664.html
上一篇:無法解決dotnetcore版本-VisualStudio
下一篇:無法從新聞網站上抓取和制作字典
