从<span>中刮取“ href”,而在<div>中没有任何类

AtApi

我对使用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>

这将为我提供标签中的所有“ 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选择器):

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章