【发布时间】:2016-04-26 09:48:33
【问题描述】:
body 标签有一个<span> 标签。 span 标签内还有很多其他的 div。我想去北斗但是当我尝试这段代码时:
from bs4 import BeautifulSoup
from urllib.request import urlopen
url = 'https://www.instagram.com/artfido/'
data = urlopen(url)
soup = BeautifulSoup(data, 'html.parser')
result = soup.body.span
print (result)
结果就是这样:
<span id="react-root"></span>
如何访问 span 标签内的 div?
我们可以解析<span> 标签吗?是否可以?如果是,那为什么我无法解析跨度?
通过使用这个:
result = soup.body.span.contents
输出是:
[]
【问题讨论】:
-
您需要将网址提供给我们,以便我们试用您的代码
-
无法测试,我有一个限制性代理...但你应该这样做
soup = BeautifulSoup( data.read(), 'html.parser') -
得到一个结果很奇怪,或者函数可能与python2不同
-
为什么?!无需使用
data.read()。对吧?! -
在 python 2 中至少是的,因为数据只是一个对象。来自文档:
If all went well, a file-like object is returned
标签: python html web-crawler