html_text = driver.page_source
soup = BeautifulSoup(html_text, "html.parser")
get_details = soup.find_all('li', attrs={"class":"news"})
# get_details is an aggregation of results fetched by BeautifulSoup find_all() method
结果集的一个实例如下:
<li class="news">blah blah blah what i want blah blah blah <a href="/graphic/graphicInfoData/000002230030421305">View details</a></li>
我想要的是“等等等等,我想要等等”,BeautifulSoup中所谓的Navigable字符串。但是,即使使用print(get_details [0] .string),我也无法对列表使用.string属性,结果为None,为什么?
顺便说一下,作为比较,下面的代码有效!
print(get_details[0].a.string)
>>> print(get_details[0].li.string)
Traceback (most recent call last):
File "<pyshell#57>", line 1, in <module>
print(get_details[0].li.string)
AttributeError: 'NoneType' object has no attribute 'string'
任何想法将不胜感激!
使用.get_text()
代替.string
:
print(get_details[0].a.get_text())
输出: View details
print(get_details[0].get_text())
输出: blah blah blah what i want blah blah blah View details
请注意,这get_details[0].get_text()
将获取的所有文本li
。
以下将仅获得第一部分:
get_details[0].contents[0].strip()
输出: blah blah blah what i want blah blah blah
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句