python - 遍历 URL 列表并抓取 tspan 元素
问题描述
我想知道是否有一种方法可以遍历 URL 列表,并从每个 URL 导入名为“tspan”的 HTML 元素。
我怎样才能做到这一点?非常感谢。
解决方案
BeautifulSoup
如果您已经下载了页面的源代码,则可以使用html
。否则,使用urllib.request.urlopen
获取页面的来源。
from bs4 import BeautifulSoup as bs
html = """
<div>
<g transform="translate(-128.8249969482422,-7.941666603088379)">
<text text-anchor="left" style="; fill:#000;">
<tspan dy="1em" </tspan>
</text>
</g>
<g transform="translate(-128.8249969482422,-7.941666603088379)">
<text text-anchor="left" style="; fill:#000;">
<tspan dy="1em" 2</tspan>
</text>
</g>
</div>"""
soup = bs(html)
tspans = soup.find_all("tspan")
tspans
[<tspan dy="1em" x="1"></tspan>,
<tspan dy="1em" x="1"></tspan>]
texts = [tspan.text for tspan in tspans]
texts
推荐阅读
- android - /dev/i2c文件的Android权限
- reactjs - ReferenceError:找不到变量:ReactJS(Safari 浏览器)中的映射
- c++ - 如何解密字母数字形式的 AES 加密消息?在下面的代码中,加密给出了字母数字。但解密是错误的
- java - 如何将字符串“指令和逗号分隔的参数”解析为Java中适当的指令实现类?
- c++ - 通过引用或指针传递 char 或 short 是否比通过值传递慢?
- python - 如何在 PyQt5 中调用或激活 MenuBar 快捷方式?
- json - 从 json 文件中选择特定项目以防多个字段具有相同的名称 vba
- android - 多次按下片段调用按钮问题
- css - 如何使用 sass 将 -fluid 添加到具有子名的类中
- bash - gcloud 命令行重定向到标准输出