【发布时间】:2020-12-08 13:59:51
【问题描述】:
我正在尝试编写一个 Python3 网络抓取工具,用于从网站中提取 a 标记内的文本。
我正在使用带有此代码的 bs4 库:
from bs4 import BeautifulSoup
import requests
req = requests.get(mainUrl).text
soup = BeautifulSoup(req, 'html.parser')
for div in soup.find_all('div', 'turbolink_scroller'):
for a in div.find_all('a', href=True, text=True):
print(a.text)
我遇到的唯一问题是它只能找到具有这种语法的文本:
<div class="test">
<a href="/link/to/whatIwant">Text that i want</a>
</div>
但不是这个:
<div class="test">
<a href="/link/to/whatIwant2">
The text
<br>
I would like
</a>
</div>
你能解释一下为什么吗?两者有什么区别?
【问题讨论】:
标签: html python-3.x web-scraping beautifulsoup