您的位置:首页 > 编程语言 > Python开发

python 抓取页面不显示图片问题

2016-05-19 12:44 316 查看
import sys, urllib
import urllib2

query='abc'
query = urllib.quote(query)

url = "http://wap.sogou.com/web/searchList.jsp?keyword=" + query
#url = "https://www.sogou.com/web?query=" + query
timeout = 30
headers = {
'User-Agent':'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1',
'Accept':'image/webp,image/*,*/*;q=0.8'
}

req=urllib2.Request(url,None,headers)
res=urllib2.urlopen(req,None,timeout)
ret=res.read();
ret = ret.replace("function postHTML(html){", "function postHTML(html){return;")
fp = open("web.html","w") #?
#fp.write(re.sub(r'(<[^>]*html[^>]*>)', r"\1<base href='"+f.url+"' />", ret, 1))
fp.close()


python抓取搜索结果页面的代码,需要加入上面的正则表达式(加入 <base href/> 标签)

或者加入

<!doctype html><base href='http://wap.sogou.com/web/searchList.jsp?dbg=off&keyword=%E5%B8%8C%E5%B0%94%E8%96%871.5' /><html>

base href 标签 地址 base href 一般放在<head> </head> 之间
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: