[编辑]
我使用的是googleappengine,我试图解析HTML内容以提取一些信息。我使用的代码是:
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.api import urlfetch
import BeautifulSoup
class MainHandler(webapp.RequestHandler):
def get(self):
url = 'http://ascodevida.com/ultimos'
result = urlfetch.fetch(url=url)
# ADVS de esta página.
res = BeautifulSoup.BeautifulSoup(result.content).findAll('div', {'class' : 'box story'})
ADVList = []
for i in res:
story = i.find('a', {'class' : 'advlink'}).string
link = i.find('a', {'class' : 'advlink'})['href']
ADVData = {
'adv' : story,
'link' : link
}
ADVList.append(ADVData)
self.response.headers['Content-Type'] = 'text/html; charset=UTF-8'
self.response.out.write(ADVList)
这个代码会产生一个奇怪字符的响应。我尝试过使用BeautifulGroup库的prettify()和renderContent()方法,但效果不佳。在
有什么解决办法吗?再次感谢。在
我认为您是直接打印列表,它调用repr,默认输出为十六进制格式(如\xe1)。在
你可以试试这个:
但是print语句将尝试解码字符串:
如果您希望得到正确的结果,只需避免默认的list行为,并自行处理每一项。在
我是一名java开发人员,使用jsoup进行HTML解析。我在python上找到了类似的方法。这可以帮助您节省时间。在
http://www.crummy.com/software/BeautifulSoup/
大脑食物: Python regular expression for HTML parsing (BeautifulSoup)
相关问题 更多 >
编程相关推荐