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>songs = [song['href'] for song in soup.select('table a')] </code></pre> <p>输出:</p> <pre><code>['/lyric/15183453/Make+You+Feel+My+Love', '/lyric/15183454/Painting+Pictures'] </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
尝试加密和解密文本文件(Python)
6 回答
尝试加密文本文件会导致类型错误
5 回答
尝试加密时溢出错误
2 回答
尝试加载.plist文件时出现“无此类文件或目录”错误
6 回答
尝试加载“blog.templatetags.blog_标记”时引发ImportError:没有名为“markdown”的模块
6 回答
尝试加载“menus.templatetags.menus\u标记”时引发ImportError:无法从“menus.models”导入名称“menus”
3 回答
尝试加载5GB文本fi时发生Python内存错误
1 回答
尝试加载5GB文本文件时出现MemoryError
4 回答
尝试加载Django模板时出现渲染错误
5 回答
尝试加载exi时出错
8 回答
尝试加载imag时使用精灵表失败
4 回答
尝试加载json.Python时出错
2 回答
尝试加载json文件时遇到此错误:json.decoder.jsondeCoderror:预期值:第1行第1列(char 0)
8 回答
尝试加载keras和tensorflow,我仍然得到ModuleNotFoundError:在我的Anaconda环境中没有名为'tensorflow_core.estimator'的模块
8 回答
尝试加载Librosa的示例fi时出现“找不到文件”错误
2 回答
尝试加载matplotlib时发生iPython笔记本错误
4 回答
尝试加载MP3时出现PyGame错误
6 回答
尝试加载pickled matplotlib figu时发生AttributeError
2 回答
尝试加载pickle文件时出错
9 回答
尝试加载Pytoch模型时遇到问题:“在模块中找不到标识”
6 回答