How python reptile set random UA

A, - Agent-setting the User
. 1, when creating the Request object headers specified
URL = ' http://ip.zdaye.com/ '
head} = {
head [ 'the User-- Agent'] = 'the Mozilla / 5.0 (the Linux; the Android 4.1 .1; the Nexus. 7 the Build / JRO03D) AppleWebKit / 535.19 (KHTML, like the Gecko) the Chrome / 18.0.1025.166 Safari / 535.19 '
REQ = request.Request (URL, headers = head)
# Request object passing the created
response = request .urlopen (REQ)
. response.read HTML = () decode ( 'UTF-. 8')
Print (HTML)
2, Request object created using the add_header
req.add_header ( 'the User-- Agent', 'the Mozilla / 5.0 (the Linux; android 4.1.1; Nexus 7 Build / JRO03D ) AppleWebKit / 535.19 (KHTML, like Gecko) Chrome / 18.0.1025.166 Safari / 535.19 ')

Parameters (key, value)

Second, set the proxy IP
after use install_opener method, the program will replace the default method urlopen. In other words, after you use install_opener, in this file, called again urlopen will use to create their own good opener. If you do not want to replace, just want to use it temporarily, you can use opener.open (url), it will not be the default program urlopen affected.
urllib Import Request from
IF name == " main ":
url = www.16yun.cn '
# This is the agents billion cattle cloud proxy IP
Proxy = {' HTTP ':' 168.68.8.88:66666 '}
# create ProxyHandler
proxy_support = Request .ProxyHandler (Proxy)
# create opener
opener = request.build_opener (proxy_support)
# Angent add the User
opener.addheaders = [( 'the User-Agent', 'Mozilla / 5.0 (Windows NT 6.1; Win64; x64) AppleWebKit / 537.36 (KHTML , like the Gecko) the Chrome / 56.0.2924.87 Safari / 537.36 ')]
# mounting opener
request.install_opener (opener)
# Uses its own installed Opener
Response = request.urlopen (URL)
HTML response.read = (). Decode ( "UTF-. 8")
Print (HTML)

Guess you like

Origin blog.51cto.com/14400115/2418658