我是新手,我开始使用BeautifulSoup和Python开发,并且我希望以全文形式获取结果,而不使用任何HTML标记或其他非文本元素。使用BeautifulSoup解析并使用特殊格式获得结果
我这样做是使用Python:
#!/usr/bin/env python
import urllib2
from bs4 import BeautifulSoup
html_content = urllib2.urlopen("http://www.demo.com/index.php")
soup = BeautifulSoup(html_content, "lxml")
# COMMENTS COUNT
count_comment = soup.find("span", "sidebar-comment__label")
count_comment
count_comment_final = count_comment.find_next("meta")
# READ COUNT
count_read = soup.find("span", "sidebar-read__label js-read")
count_read
count_read_final = count_read.find_next("meta")
# PRINT RESULT
print count_comment_final
print count_read_final
我的HTML看起来像这样:
<div class="box">
<span class="sidebar-comment__label">Comments</span>
<meta itemprop="interactionCount" content="Comments:115">
</div>
<div class="box">
<span class="sidebar-read__label js-read">Read</span>
<meta itemprop="interactionCount" content="Read:10">
</div>
,我得到这个:
<meta content="Comments:115" itemprop="interactionCount"/>
<meta content="Read:10" itemprop="interactionCount"/>
我会得到这样的:
You've 115 comments
You've 10 read
首先,这可能吗?
其次,我的代码好吗?
第三,你能帮助我吗? ;-)
差不多完成了,它会显示 “注释” 和 “读”,而不是 “115” 和 “10”。 – TwinyTwice 2014-09-25 05:18:41
使用'split(':')[1]'。对不起 – nu11p01n73R 2014-09-25 05:20:17