使用BeautifulSoup,我希望从以下代码中提取数据:
<div class="property-tag-container">
<span class="c-prop-tag2">距离8号线市光路站284米</span>
<span class="c-prop-tag2">满五</span>
<span class="c-prop-tag2">有钥匙</span>
</div>我使用的代码如下:
for i in range (1,3):
if i == 1:
i = str(i)
a = (url + page + i + '/')
r = requests.get(url=a, headers=headers)
html = r.content
print(type(html),html)
else:
i = str(i)
a = (url + page + i + '/')
r = requests.get(url=a, headers=headers)
html2 = r.content
html = html + html2
lj=BeautifulSoup(html,'html.parser')
subway=lj.find_all('div',attrs={'class':'property-tag-
container'},limit=None)
sb=[]
for c in subway:
subway=c.string
sb.append(subway)
print(len(sb),sb)但是,列表sb中没有任何内容,因此输出如下:
60无,无,无
发布于 2017-09-29 15:41:26
这个类只有一个div,它的string属性是None。
试试这个:
for c in subway:
subway=c.get_text()
sb.append(subway)https://stackoverflow.com/questions/46492348
复制相似问题