在 BeautifulSoup / Python 中,如何从结果集中提取单个元素?
In BeautifulSoup / Python, how do I extract a single element from a result set?
我正在使用 Python 3.7 和 BeautifulSoup 4。我在从元素获取文本时遇到问题。我正在尝试这个
req = urllib2.Request(fullurl, headers=settings.HDR)
html = urllib2.urlopen(req, timeout=settings.SOCKET_TIMEOUT_IN_SECONDS).read()
bs = BeautifulSoup(html, features="lxml")
...
author_elts = bs.find_all("a", class_="author")
author_elt = author_elts.first
但是在 "author_elt = author_elts.first" 行,我得到了错误
AttributeError: ResultSet object has no attribute 'first'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()
从 ResultSet 中提取元素的正确方法是什么?
find_all
return 一个列表,为什么不使用 author_elts[0]
获取第一个元素?
我正在使用 Python 3.7 和 BeautifulSoup 4。我在从元素获取文本时遇到问题。我正在尝试这个
req = urllib2.Request(fullurl, headers=settings.HDR)
html = urllib2.urlopen(req, timeout=settings.SOCKET_TIMEOUT_IN_SECONDS).read()
bs = BeautifulSoup(html, features="lxml")
...
author_elts = bs.find_all("a", class_="author")
author_elt = author_elts.first
但是在 "author_elt = author_elts.first" 行,我得到了错误
AttributeError: ResultSet object has no attribute 'first'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()
从 ResultSet 中提取元素的正确方法是什么?
find_all
return 一个列表,为什么不使用 author_elts[0]
获取第一个元素?