第一种 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