【发布时间】:2016-12-24 18:18:24
【问题描述】:
我是 Python 的初学者,我正在尝试实现一个 webscraper 来抓取一些调查数据。我正在尝试使用 nth-of-type CSS 选择器(因为那是 BeautifulSoup 让我使用的唯一伪类)来选择作为父元素的第 7 个元素的所有元素(即,如果您访问调查,这就是全部平均分)。我在下面编写了抛出 NotImplementedError 的代码,即使我已经在 http://jsfiddle.net/3Ycu9/ 中测试了选择器并且我只使用了 nth-of-type 和属性选择器。有人可以帮我弄清楚为什么会出现此错误吗?
import requests, bs4
res = requests.get('http://www.eecs.umich.edu/eecs/undergraduate/survey/all_survey.2016.htm')
res.raise_for_status()
survey = bs4.BeautifulSoup(res.text, "html.parser")
classes = survey.select('td[colspan=3]')
# select the 7th <td> element in every <tr> tag
difficulty = survey.select('td[style*="border-top:none;border-left:none"]:nth-of-type(7)')
for i in range(len(difficulty)):
print(str(difficulty[i].getText()))
【问题讨论】:
标签: html python-3.x web-scraping css-selectors beautifulsoup