我正在写一个使用python for android (sl4a)的安卓应用程序,我想让它做的是搜索一个笑话网站并提取一个笑话。那就告诉我那个笑话来唤醒我吧。到目前为止,它将原始的html源代码保存到一个列表中,但我需要它来创建一个新的列表,方法是保存html标记之间的数据,然后将该数据读给我。这是我不能使用的解析器。代码如下:
import android
droid = android.Android()
import urllib
current = 0
newlist = []
sock = urllib.urlopen("http://m.funtweets.com/random")
htmlSource = sock.read()
sock.close()
rawhtml = []
rawhtml.append (htmlSource)
while current < len(rawhtml):
while current != "<div class=":
if [current] == "</b></a>":
newlist.append (current)
current += 1
print newlist发布于 2014-04-14 13:18:29
使用这个库来解析android http://jsoup.org/中的HTML,它的影响范围和开发人员广泛接受的库,它也可用于python :)
发布于 2014-04-23 03:40:38
这是如何做的:代码导入重新导入urllib2
page = urllib2.urlopen("http://www.m.funtweets.com/random").read()
user = re.compile(r'<span>@</span>(\w+)')
text = re.compile(r"</b></a> (\w.*)")
user_lst =[match.group(1) for match in re.finditer(user, page)]
text_lst =[match.group(1) for match in re.finditer(text, page)]
for _user, _text in zip(user_lst, text_lst):
print '@{0}\n{1}\n'.format(_user,_text)/code
https://stackoverflow.com/questions/23052177
复制相似问题