【发布时间】:2020-12-17 18:29:35
【问题描述】:
我对使用 BeatifulSoup 和 python 进行抓取非常陌生,我在尝试在跨度内获取 href 但它没有类时遇到了一些困难。以下代码部分来自 phpbb 论坛,我没有问题抓取所有的href,但由于某种原因,我无法弄清楚如何获取跨度内的内容..
<div class="col-md-48 post-text" data-topic="6693rw38" data-forum="2">
<br>
<br>
<a href="http://imgshare.net/img-5ba3dt3ad8a24.html" target="_blank" class="postlink" rel="nofollow"></a>
<br>
<br>
<a href="http://imgshare.net/img-5baefr1a51a49.html" target="_blank" class="postlink" rel="nofollow"></a>
<br>
<br>
<span>
<a href="https://k2s.cc/file/5c745ce5g9193/toyota.mp4" target="_blank">https://k2s.cc/file/5c745ce5g9193/toyota.mp4</a>
</span>
<br>
<br>
<a href="http://imgshare.net/img-5ba34d1q805b8.html" target="_blank" class="postlink" rel="nofollow"></a>
<br>
<br>
<span>
<a href="https://k2s.cc/file/b28gr283ef76e/ford.mp4" target="_blank">https://k2s.cc/file/b28gr283ef76e/ford.mp4</a>
</span>
这将为我提供 a 标签内的所有“href”:
url ='somephpbbforum.com'
page = requests.get(url)
soup = BeautifulSoup(page.content, 'lxml')
link = soup.find_all('div', class_ = 'col-md-48')
for div in link:
all_links = [link1['href'] for link1 in div.find_all('a')]
print(all_links)
谢谢大家!
【问题讨论】:
标签: python web-scraping