标签:pre safari add url 信息 lib opener htm 利用
方法1:使用build_opener()来添加
import urllib2 url = "https://www.baidu.com" headers = ("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36") opener = urllib2.build_opener() opener.addheaders = [headers] file = opener.open(url) html = file.read() print html
方法2:利用add_header()来添加
import urllib2 url = "https://blog.51cto.com/lsfandlinux/2046467" req = urllib2.Request(url) req.add_header("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36") file = urllib2.urlopen(req) html = file.read() print html
标签:pre safari add url 信息 lib opener htm 利用
原文地址:https://www.cnblogs.com/lanxy/p/11876049.html