Python中文
首页
教程
问答
标签
搜索
登录
注册
Python中的Web抓取如何捕获所有<a>元素
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我正在使用<code>beautifulsoup4</code>从歌词网网站,特别是这个链接:<a href="https://www.lyrics.com/album/1447935" rel="nofollow noreferrer">https://www.lyrics.com/album/1447935</a>。你知道吗</p> <p>从这个块中,我试图提取两个<code><a></code>元素:</p> <pre><code>[<table class="tdata"> <colgroup> <col style="width: 50px;"/> <col style="width: 430px;"/> <col style="width: 80px;"/> <col style="width: 80px;"/> </colgroup> <thead> <tr> <th>#</th> <th>Song</th> <th>Duration</th> <th> </th> </tr> </thead> <tbody> <tr> <td class="tal qx">1</td> <td class="tal qx"> <strong> <a href="/lyric/15183453/Make+You+Feel+My+Love">Make You Feel My Love</a> </strong> </td> <td class="tal qx">3:32</td> <td class="tal vam rt"> </td></tr><tr><td class="tal qx">2</td> <td class="tal qx"> <strong> <a href="/lyric/15183454/Painting+Pictures">Painting Pictures</a> </strong> </td> <td class="tal qx">3:33</td> <td class="tal vam rt"> </td> </tr> </tbody> </table>] </code></pre> <p>这是我的密码:</p> <pre><code>url = "http://www.lyrics.com" + album_url page = r.get(url) soup = bs(page.content, "html.parser") songs = [a.get('href') for a in (table.find('a') for table in soup.findAll('table')) if a] </code></pre> <p>但是,它只返回第一个<code><a></code>:</p> <pre><code>['/lyric/15183453/Make+You+Feel+My+Love'] </code></pre> <p>有什么问题吗?你知道吗</p> <p>编辑:谢谢大家的回答!我投了更高的票,但我没有足够的代表来展示</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>能够使它与:</p> <pre><code>for a in soup.findAll('a'): if a.parent.name == 'strong': if a.parent.parent.name == 'td': print(a["href"]) </code></pre> <p>不过,我仍然不知道为什么其他方法不起作用,因为我在程序的其他地方使用过它,没有任何问题。你知道吗</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何在python中从数据帧列中删除分类值?
9 回答
如何在python中从数据帧列表中删除引号
9 回答
如何在python中从数据帧创建列表
1 回答
如何在Python中从数据帧创建嵌套的JSON
6 回答
如何在Python中从数据帧显示wordcloud
2 回答
如何在Python中从数据帧的时间戳中删除字符
10 回答
如何在Python中从数据帧绘制简单绘图?
10 回答
如何在python中从数据帧行提取具有特定长度的范围?
5 回答
如何在python中从数据帧设置dict中的值
8 回答
如何在Python中从数据库中获得一个结果
9 回答
如何在python中从数据框中绘制分类条形图
10 回答
如何在Python中从数据框中选择特定细节?
3 回答
如何在python中从数据集中删除unicode
6 回答
如何在python中从数据集中删除某些数值?
5 回答
如何在python中从数据集中选择行
8 回答
如何在Python中从数组中删除元素
5 回答
如何在python中从数组中删除单个倒逗号?
9 回答
如何在python中从数组中删除对象?
3 回答
如何在python中从数组中删除引号
7 回答
如何在python中从数组中删除所有最小值
2 回答