我正在使用BeautifulSoup通过“ reply_to”类查找href。
<div class="message">
<div class="reply_to details">
In reply to <a href="#go_to_message18" onclick="return
GoToMessage(18)">this message</a>
</div>
</div>
现在下面的代码可以找到reply_to元素,但接下来是如何查找
soup = BeautifulSoup(parsed.input[0], "html.parser")
alldefaultmesssages = soup.select(".message.default")
alldefaultmesssages.select_one(".reply_to")
答案 0 :(得分:1)
您需要进入anchor
元素的子reply_to
标签:
import requests
from bs4 import BeautifulSoup
html = """<div class="message">
<div class="reply_to details">
In reply to <a href="#go_to_message18" onclick="return
GoToMessage(18)">this message</a>
</div>
</div>"""
soup = BeautifulSoup(html, 'html.parser')
print(soup.find("div", class_="reply_to").a.get('href'))
#>>> #go_to_message18