python - 解析表中的文本不起作用

标签 python beautifulsoup python-requests

我正在尝试在标签中获取以下文本。

<td align="center" valign="top">I AM TRYING TO GET THIS</td>

这是在一个表中,这个特定的文本是特定的行和列,我试图将其与该列的其余部分一起获取。

我尝试过 for 循环,也尝试过这个:

r = driver.get("url")

htmltext = htmlfile.read()

regex = '<td align="center" valign="top">(.+?)</td>'

pattern = re.compile(regex)

grade = re.findall(pattern,htmltext)

print(grade)

我更喜欢 BS4 方式来做到这一点。

最佳答案

我无法检查它,但它应该可以工作

import requests
from bs4 import BeautifulSoup

url = 'http://www.w3schools.com/html/html_tables.asp'

r = requests.get(url)

soup = BeautifulSoup(r.text, 'html.parser')

for x in soup.find_all('td'): 
    print(x.text.strip())

关于python - 解析表中的文本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40705341/

相关文章:

python - 如何将二元类 Logistic 回归与 Python 合并

python - 在 Raspberry Pi 上找不到 ttyUSB0

python - 查找包含文章的子页面 URL 并从中收集数据

python - 如果我没有在 requests.get() 中指定用户代理会发生什么?

Python,请求,错误= http.client.BadStatusLine : <ServerStats>

google-maps - Google API 上的 Google API 配额自动完成

python - py.test 不从数据库中提取数据

python - urllib2.URLError : <urlopen error Tunnel connection failed: 403 Tunnel or SSL Forbidden>

python - 从 Beautifulsoup4 获取字符串时出现问题

Python + BeautifulSoup : How to get ‘href’ attribute of ‘a’ element?