Python模拟浏览器实现网页访问
生活随笔
收集整理的這篇文章主要介紹了
Python模拟浏览器实现网页访问
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
模擬瀏覽器請求數據:
import socket# 創建TCP鏈接 tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # DNS 解析和鏈接HTTP服務器 tcp_socket.connect(("www.qq.com", 80))# 編寫請求頭, 發送HTTP請求報文 # 請求行 request_line = "GET / HTTP/1.1\r\n" # 請求頭,設置請求域名 request_header = "www.qq.com\r\n" request_data = request_line + request_header + "\r\n"# 發送請求 tcp_socket.send(request_data.encode("utf-8")) # 接收響應報文,指定長度 response_data = tcp_socket.recv(4096) # 對響應報文進行解析 --切割 response_str_data = response_data.decode("utf-8") print(response_data) # "\r\n\r\n" 之后的數據就是響應體數據 index = response_str_data.find("\r\n\r\n") # 切割出的數據就是文件數據 html_data = response_str_data[index+4:] # 在當前路徑下新建文件存儲響應數據 with open("index.html", "wb") as file:file.write(html_data.encode()) # 關閉套接字 tcp_socket.close()轉載于:https://blog.51cto.com/10412806/2095303
總結
以上是生活随笔為你收集整理的Python模拟浏览器实现网页访问的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql版本选择最终建议
- 下一篇: Webpack 4x 之路 ( 五 )