python - 无法使用 beautifulsoup 解析 python 中的 HTML

标签 python html python-2.7 beautifulsoup html-parsing

我对编程和Python都是新手。请耐心等待。 :) 我试图解析的链接是 http://results.vtu.ac.in/vitavi.php?rid=1JS10CS007&submit=SUBMIT

在该链接上;我需要刮掉一些东西,我已经在这篇文章所附的图片中标记了它们。我无法自己完成此操作,因为页面的代码编写/组织得不好。 请在这方面帮助我。谢谢。

我编写了一个程序来获取页面的代码。这是:

from bs4 import BeautifulSoup
from urllib2 import urlopen
mylink = "http://results.vtu.ac.in/vitavi.php?rid=1JS10CS007&submit=SUBMIT"
pagetext = urlopen(mylink).read()
soup = BeautifulSoup(pagetext)
print soup.prettify()

最佳答案

假设你想获取结果表的内容。

没有面向数据的类或 ID,并且有大量嵌套表,使得查找所需数据变得更加困难。

我会找到Subject元素并找到第一个父table。然后,迭代行和单元格并获取所需的数据:

from urllib2 import urlopen

from bs4 import BeautifulSoup

url = "http://results.vtu.ac.in/vitavi.php?rid=1JS10CS007&submit=SUBMIT"

soup = BeautifulSoup(urlopen(url))
results_table = soup.find(text="Subject").find_parent("table")

for row in results_table.find_all("tr"):
    print [cell.get_text(strip=True) for cell in row.find_all("td")]

打印:

[u'Subject', u'External', u'Internal', u'Total', u'Result']
[u'Software Architectures (10IS81)', u'46', u'21', u'67', u'P']
[u'System Modeling and Simulation (10CS82)', u'45', u'15', u'60', u'P']
[u'Software Testing (10CS842)', u'41', u'15', u'56', u'P']
[u'Project Work (10CS85)', u'95', u'97', u'192', u'P']
[u'Information and Network Security (10CS835)', u'39', u'20', u'59', u'P']
[u'Seminar (10CS86)', u'0', u'44', u'44', u'P']

关于python - 无法使用 beautifulsoup 解析 python 中的 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31434404/

相关文章:

javascript - 旋转木马上的第 N 个 child 不工作

python - Matplotlib 行高表属性

html - float 图像在相对位置上不在同一高度

python - 我在使用 im.show() 时遇到错误

mysql - Python 中多表删除的正确 MySQL 查询

python - 如何让 BeautifulSoup 4 遵守自动关闭标签?

python numpy`函数数组(lambda函数)

Python子进程执行脚本不会写入文件

IIS 中的 Python CGI : issue with urandom function

html - 带有必填字段的 Rails 下拉菜单