Home  >  Article  >  Backend Development  >  How to implement automatic access to web pages in Python

How to implement automatic access to web pages in Python

零到壹度
零到壹度Original
2018-04-14 14:08:215351browse


The content of this article is to share with you how Python implements the function of automatically accessing web pages. It is mainly displayed in the form of code and has certain reference value. Friends in need can Please refer to

import urllib.request
import requests
import time
import ssl
import random

def openUrl(ip, agent):
    headers = {'User-Agent': agent}    
    proxies = {'http' : ip}    
    requests.get("https://www.baidu.com", headers=headers, proxies=proxies, verify=True)    
    ssl._create_default_https_context = ssl._create_unverified_context    
    print("Access to success.")
    
#IP池
#IP来源: 
def randomIP():
    ip = random.choice(['120.78.78.141', '122.72.18.35', '120.92.119.229'])    
    return ip
    
#User-Agent
#User-Agent来源: 
def randomUserAgent():
    UserAgent = random.choice(['Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36',                              
                               'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36',                              
                               'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36'])    
    return UserAgent
    
if __name__ == '__main__':
    for i in range(10):   
         ip = randomIP()        
         agent = randomUserAgent()        
         openUrl(ip, agent)        
         time.sleep(1)

related recommendations:

Usage of WebSpec automatic access page

Simulating HTTP requests to realize automatic web page operation and data collection

The above is the detailed content of How to implement automatic access to web pages in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn