从 a 中没有类的 中抓取'href' <span> <div>



我是BeatifulSoup和python的新手,在尝试在span中获取href时遇到了一些困难,但它没有类。。以下部分代码来自phpbb论坛,我可以抓取所有href,但由于某种原因,我无法弄清楚如何抓取span内部的内容。。

<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>

这将给我一个标签中的所有"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)

谢谢大家!

您可能正在寻找类似的东西(使用css selctors(:

all_links = [s['href'] for s in soup.select('div.col-md-48 > a[href]')]
all_links

输出:

['http://imgshare.net/img-5ba3dt3ad8a24.html',
'http://imgshare.net/img-5baefr1a51a49.html',
'http://imgshare.net/img-5ba34d1q805b8.html']

编辑:

要获取这些节点的文本内容,请使用

all_links2 = [s.text for s in soup.select('div.col-md-48 > span > a[href]')]
all_links2

输出:

['https://k2s.cc/file/5c745ce5g9193/toyota.mp4',
'https://k2s.cc/file/b28gr283ef76e/ford.mp4']

最新更新