无法使用beautifulsoup在python中删除链接

问题描述 投票:2回答:1

我想废弃标签和class =“author track”的网页上的所有链接。在网页上有这样的多个URL,但是当我运行程序时,我的列表是空的

一个HTML示例: -

<a class="author track" href="/nileshkikuuchise" data-gaq="author" data-dmc="entry-artist">
                                                                        <img class="avatar" src="https://ctl.s6img.com/cdn/s6-original-art-uploads/society6/uploads/u/nileshkikuuchise/avatar_asset/5323d6c4d92143e8b37f0fa644d7044f_p3.jpg" width="20" height="20" data-dmc="entry-photo">
                                                                    Nileshkikuuchise                                </a>

我的代码: -

discover_page = BeautifulSoup(r.text, 'html.parser')
finding_accounts = discover_page.find_all("a", "[class~=author track]")
print(finding_accounts)

输出是无

如何将href值输入列表?我可以稍后进行for循环,但需要先弄清楚基础知识

python python-3.x web-scraping beautifulsoup
1个回答
0
投票

你似乎混合了selectfind_all所期望的风格。

这两种方法对我有用:

>>> r = '''
<a class="author track" href="/nileshkikuuchise" data-gaq="author" data-dmc="entry-artist">
                                                                        <img class="avatar" src="https://ctl.s6img.com/cdn/s6-original-art-uploads/society6/uploads/u/nileshkikuuchise/avatar_asset/5323d6c4d92143e8b37f0fa644d7044f_p3.jpg" width="20" height="20" data-dmc="entry-photo">
                                                                    Nileshkikuuchise                                </a>
'''

>>> discover_page = BeautifulSoup(r, 'html.parser')
>>> discover_page.find_all("a", class_="author track")
[<a class="author track" data-dmc="entry-artist" data-gaq="author" href="/nileshkikuuchise">
 <img class="avatar" data-dmc="entry-photo" height="20" src="https://ctl.s6img.com/cdn/s6-original-art-uploads/society6/uploads/u/nileshkikuuchise/avatar_asset/5323d6c4d92143e8b37f0fa644d7044f_p3.jpg" width="20"/>
                                                                     Nileshkikuuchise                                </a>]
>>> discover_page.select('a[class="author track"]')
[<a class="author track" data-dmc="entry-artist" data-gaq="author" href="/nileshkikuuchise">
 <img class="avatar" data-dmc="entry-photo" height="20" src="https://ctl.s6img.com/cdn/s6-original-art-uploads/society6/uploads/u/nileshkikuuchise/avatar_asset/5323d6c4d92143e8b37f0fa644d7044f_p3.jpg" width="20"/>
                                                                     Nileshkikuuchise                                </a>]
© www.soinside.com 2019 - 2024. All rights reserved.