第一种 User-Agent方法:
import urllib2
if __name__ == '__main__' :
url = '想访问的网址'
head ={}
head['User-Agent'] = 'Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19'
req = urllib2.Request(url,headers=head)
response = urllib2.urlopen(req)
html = response.read().decode('utf-8') #根据网站的编码格式,选择。‘UTF-8’,‘GBK’等
print html
第二种 User-Agent 方法:
import urllib2
if __name__ == '__main__':
url = '想访问的网址'
req = urllib2.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19')
response = urllib2.urlopen(req)
html = response.read().decode('utf-8')
print html
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。