python reptile simulation browser to access -User-Agent

Analog browser access -User-Agent:

Import urllib2 

# the User-Agent simulation browser to access the 
headers = { " the User-Agent " : " Mozilla / 5.0 (Windows NT 10.0; Win64; x64) AppleWebKit / 537.36 (KHTML, like Gecko) Chrome / 79.0.3945.88 Safari / 537.36 " } 

# a request object by () method is configured urllib2.Request 
request urllib2.Request = ( " http://www.baidu.com/ " , headers = headers) 


# sends a request to the specified URL, and the server returns a response file-like object 
# response class file server response is, in addition to the method of support file operations, the following types commonly used methods: 
response = urllib2.urlopen (Request) 



# file-like object supports a method of operating the server returns the file object python 
# read () method is to read the entire contents of the file, returns the string
= HTML response.read () 

# returns the HTTP response code, the server returns successfully 200,4 page faults; 5 server issues 
Print response.getcode () 

# returns the actual URL of the actual data, to prevent redirects 
Print response.geturl ( ) 

# returns the server's HTTP response headers 
Print response.info () 


# print the response content 
Print (HTML)

Guess you like

Origin www.cnblogs.com/moongo/p/12105259.html