Python實(shí)現(xiàn)自動(dòng)訪問(wèn)網(wǎng)頁(yè)的例子
我就廢話不多說(shuō)了,直接上代碼吧!
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來(lái)源:
# http://www.xicidaili.com/
# https://www.kuaidaili.com/free/
def randomIP():
ip = random.choice(['120.78.78.141', '122.72.18.35', '120.92.119.229'])
return ip
#User-Agent
#User-Agent來(lái)源:http://www.useragentstring.com/pages/useragentstring.php
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)
補(bǔ)充拓展:Python模擬瀏覽器實(shí)現(xiàn)網(wǎng)頁(yè)訪問(wèn)方式
模擬瀏覽器請(qǐng)求數(shù)據(jù):
import socket
# 創(chuàng)建TCP鏈接
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# DNS 解析和鏈接HTTP服務(wù)器
tcp_socket.connect(("www.qq.com", 80))
# 編寫請(qǐng)求頭, 發(fā)送HTTP請(qǐng)求報(bào)文
# 請(qǐng)求行
request_line = "GET / HTTP/1.1\r\n"
# 請(qǐng)求頭,設(shè)置請(qǐng)求域名
request_header = "www.qq.com\r\n"
request_data = request_line + request_header + "\r\n"
# 發(fā)送請(qǐng)求
tcp_socket.send(request_data.encode("utf-8"))
# 接收響應(yīng)報(bào)文,指定長(zhǎng)度
response_data = tcp_socket.recv(4096)
# 對(duì)響應(yīng)報(bào)文進(jìn)行解析 --切割
response_str_data = response_data.decode("utf-8")
print(response_data)
# "\r\n\r\n" 之后的數(shù)據(jù)就是響應(yīng)體數(shù)據(jù)
index = response_str_data.find("\r\n\r\n")
# 切割出的數(shù)據(jù)就是文件數(shù)據(jù)
html_data = response_str_data[index+4:]
# 在當(dāng)前路徑下新建文件存儲(chǔ)響應(yīng)數(shù)據(jù)
with open("index.html", "wb") as file:
file.write(html_data.encode())
# 關(guān)閉套接字
tcp_socket.close()
以上這篇Python實(shí)現(xiàn)自動(dòng)訪問(wèn)網(wǎng)頁(yè)的例子就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
利用Python實(shí)現(xiàn)手機(jī)短信監(jiān)控通知的方法
今天小編就為大家分享一篇利用Python實(shí)現(xiàn)手機(jī)短信監(jiān)控通知的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07
python中的計(jì)時(shí)器timeit的使用方法
本篇文章主要介紹了python中的計(jì)時(shí)器timeit的使用方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
python 數(shù)據(jù)類型強(qiáng)制轉(zhuǎn)換的總結(jié)
這篇文章主要介紹了python 數(shù)據(jù)類型強(qiáng)制轉(zhuǎn)換的使用總結(jié),幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-01-01
PyTorch模型創(chuàng)建與nn.Module構(gòu)建
這篇文章主要為大家介紹了PyTorch模型創(chuàng)建與nn.Module構(gòu)建示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
把vgg-face.mat權(quán)重遷移到pytorch模型示例
今天小編就為大家分享一篇把vgg-face.mat權(quán)重遷移到pytorch模型示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
Python re.split方法分割字符串的實(shí)現(xiàn)示例
本文主要介紹了Python re.split方法分割字符串的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
python獲取網(wǎng)頁(yè)中所有圖片并篩選指定分辨率的方法
下面小編就為大家分享一篇python獲取網(wǎng)頁(yè)中所有圖片并篩選指定分辨率的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
Python實(shí)現(xiàn)識(shí)別手寫數(shù)字 簡(jiǎn)易圖片存儲(chǔ)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)識(shí)別手寫數(shù)字,簡(jiǎn)易圖片存儲(chǔ)管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
python logging.info在終端沒(méi)輸出的解決
這篇文章主要介紹了python logging.info在終端沒(méi)輸出的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05

