問題

我試圖在沒有文字“詳細資訊”的情況下獲得eBay標題.我嘗試使用“next_shold”,但這不起作用.

  title_BeautifulSoup = bs.find('h1', {'id': 'itemTitle'}).next_sibling
 # What I get in return: <div id="vi-itt-filler"></div>
 

enter image description here

整個程式碼: https://pastebin.com/4NFLGDpp

  最佳答案

您可以使用find_next(‘span’)和.next_element.next_element

 title_BeautifulSoup = bs.find('h1', {'id': 'itemTitle'}).find_next('span').next_element.next_element
print(title_BeautifulSoup)
 

或內容並取索引1

 title_BeautifulSoup = bs.find('h1', {'id': 'itemTitle'}).contents
print(title_BeautifulSoup[1])
 

輸出:

 Invicta Men's Watch Pro Diver Quartz Black Dial Dive Quartz Bracelet 8932OB
 

  相同標籤的其他問題

pythonhtmlweb-scrapingbeautifulsoup