[英]Find a p tag with a partial string using beautifulsoup, and extract the integer in the string of the p tag that follows
[英]A way to extract certain <p> tag using BeautifulSoup?
我陷入了當前的問題,使用BS4從ap標簽提取文本。
我需要提取的是專門包含文本的p標簽,但是還存在其他p標簽。
我目前擁有的是:
soup2 = BeautifulSoup(response2, 'html.parser')
div = soup2.find("div", {"id": "body"}).find_all('p')
print (div[5])
我知道find_all創建了所有p標簽的列表,並且有可能找到我要查找的p標簽的列表索引。 但是,這會帶來一個問題,因為我在具有與圖片相似的HTML布局的其他頁面上多次執行了此提取操作。 與之類似,並不是每個find_all列表都會將我要查找的p標簽文本作為第5個索引。
有什么建議么?
find_all接受許多參數。
您可以使用它們更好地過濾結果。
https://www.crummy.com/software/BeautifulSoup/bs4/doc/#find-all
您還可以遍歷所有元素並找到最可能的元素。
創建100個html頁面的示例,並找到適用於所有頁面的方法/策略組合。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.