我如何使用 mechanize 和 BeautifulSoup 找到这些等级和这些 class 名称?
How would I find these grades and these class names using mechanize and BeautifulSoup?
所以,我已经想出了如何登录到允许我检查我的成绩、进度簿、python、机械化和 BeautifulSoup 的网站。但是我 运行 遇到了从 html 页面获取 类 的名称和成绩的问题。这是 类 和成绩之一:
< a class="expandable-row" href="/student/assignments/class/16152?
< span class="expandable-row">85.42 B< /span>
因此,我需要 python 搜索 html 以找到所有班级名称和所有年级。
这是完整的 html 代码:http://pastebin.com/ikaw0FmN
感谢您的帮助。
假设您知道如何通过请求获取页面,您将执行以下操作:
...
from bs4 import BeautifulSoup
...
gradetd = BeautifulSoup(html).find_all('td',{'class':'fixed-column important'})
for row in gradetd:
grades = row.find('span',{'class':'expandable-row'}).text.strip()
if grades:
avg, grade = grades.split(' ')
print("{}/{}".format(avg,grade))
89.83/A-
99.14/A+
98.20/A+
91.14/A-
94.32/A
95.76/A
91.28/A-
85.42/B
97.86/A+
95.63/A
90.16/A-
94.09/A
97.02/A+
92.64/A-
100.00/A+
100.00/A+
100.00/A+
98.18/A+
100.00/A+
所以,我已经想出了如何登录到允许我检查我的成绩、进度簿、python、机械化和 BeautifulSoup 的网站。但是我 运行 遇到了从 html 页面获取 类 的名称和成绩的问题。这是 类 和成绩之一:
< a class="expandable-row" href="/student/assignments/class/16152?
< span class="expandable-row">85.42 B< /span>
因此,我需要 python 搜索 html 以找到所有班级名称和所有年级。 这是完整的 html 代码:http://pastebin.com/ikaw0FmN 感谢您的帮助。
假设您知道如何通过请求获取页面,您将执行以下操作:
...
from bs4 import BeautifulSoup
...
gradetd = BeautifulSoup(html).find_all('td',{'class':'fixed-column important'})
for row in gradetd:
grades = row.find('span',{'class':'expandable-row'}).text.strip()
if grades:
avg, grade = grades.split(' ')
print("{}/{}".format(avg,grade))
89.83/A-
99.14/A+
98.20/A+
91.14/A-
94.32/A
95.76/A
91.28/A-
85.42/B
97.86/A+
95.63/A
90.16/A-
94.09/A
97.02/A+
92.64/A-
100.00/A+
100.00/A+
100.00/A+
98.18/A+
100.00/A+