beautifulsoup - 使用 BeautifulSoup 从 html 中提取文本
问题描述
我是 python 和 BeautifulSoup 的新手,需要帮助编写一个 for 循环来从 html 中检索一些文本值。堆栈溢出也是新的:-)
我可以使用下面的 td 标签来抓取网页,并找到包含我想要添加到列表中的公司员工的行。不知道如何编写将忽略标签的 for 循环,只从每一行检索文本值(即员工姓名),然后将其添加到新列表,员工。因此,在下面的示例中,我如何将 John Doe、Bob Smith 等检索到列表中?任何帮助表示赞赏。
import requests
from bs4 import BeautifulSoup as soup
from urllib.request import urlopen as uReq
import re
url = 'my target URL'
target_url= uReq(url)
target_html = target_url.read()
soupy = soup(target_html, 'html.parser')
print(soupy.prettify())
employees = []
employees = soupy.findAll('td', headers= 'table5593r1')
employees
<td headers="'table5593r1"><a href="https://www.acme.org/about-acme/people/john-doe" target="_blank">Mr John Doe</a></td>,
<td headers="'table5593r1"><a href="https://www.acme.org/about-acme/people/bob-smith">Dr Bob Smith</a></td>,
<td headers="'table5593r1"><a href="https://www.acme.org/about-acme/people/jane-do">Dr Jane Do</a></td>,
<td headers="'table5593r1"><a href="https://www.acme.org/about-acme/people/mary-jane">Ms Mary Jane</a></td>,
解决方案
推荐阅读
- linux - 在 Raspberry PI 中,如何实现对 SD 卡的最小写入周期并随时拔出 RPI?
- java - 无法解决“局部变量需要是最终的”
- git - git子模块 - 忽略更改
- shopify - 列出所有包含当前标签的产品(在产品页面上)
- cocoa - 来自 Cocoa 的 exiftool
- typescript - 日期的时区在日期管道中不起作用 - 角度 5
- javascript - 删除斜线并替换空格
- android - ReactJS 与 Native Android 中的 Webview 通信('Android' 未定义 no-undef)
- amazon-web-services - 发送到 SQS 的条带付款现在不发送......使用 lambda
- python-3.x - Jupyter Notebook 初学者 编写函数的问题