BeautifulSoup 善于网页数据分析 ,但是 python for android : BeautifulSoup 有 bug ,
text = h4.a.text 只能取得 None,因此我写了function: getText() 来fix this bug.
例如: 抓取优快云极客头条内容 soup.py
import urllib2, re
from BeautifulSoup import BeautifulSoup
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
def getText(text):
begin = text.find('>',0)
if begin > -1:
begin += 1
end = text.find('</a>',begin)
if begin < end:
return text[begin:end].strip()
else:
return None
else:
return None
page = urllib2.urlopen("http://geek.youkuaiyun.com/new")
soup = BeautifulSoup(page)
for h4 in soup.findAll('h4'):
if h4.a is not None:
href = h4.a.get('href')
text = getText(str(h4.a))
print text
print href
page.close()
请参考: http://www.crummy.com/software/BeautifulSoup/bs3/documentation.zh.html
本文分享了一个针对PythonforAndroid环境下BeautifulSoup库中获取特定HTML元素文本时出现的问题及解决方案。作者通过自定义getText()函数成功修复了text = h4.a.text只能返回None的Bug,并展示了如何使用该函数抓取优快云极客头条的内容。
2897

被折叠的 条评论
为什么被折叠?



