1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 获取网页源代码 python_python 获取网页源代码

获取网页源代码 python_python 获取网页源代码

时间:2023-03-18 19:32:41

相关推荐

获取网页源代码 python_python 获取网页源代码

import re #正则表达式模块

import urllib.request

import time #时间模块

import string #字符串模块

def getHtml(url):f=urllib.request.urlopen(url)

print(f.read())

if name == '__main__':

getHtml('/')

下面的是获取网页代码不乱码

import re #正则表达式模块

import urllib.request

import time #时间模块

import string #字符串模块

def getHtml(url):f=urllib.request.urlopen(url)

print(str(f.read(),'utf-8'))

if name == '__main__':getHtml('/')

第三种获取网页源代码方法

import requests

req = requests.get("/")

if req.encoding == 'ISO-8859-1':encodings = requests.utils.get_encodings_from_content(req.text)

if encodings:

encoding = encodings[0]

else:

encoding = req.apparent_encoding

# encode_content = req.content.decode(encoding, 'replace').encode('utf-8', 'replace')

global encode_content

encode_content = req.content.decode(encoding, 'replace') #如果设置为replace,则会用?取代非法字符;

print(encode_content)

with open('test.html','w',encoding='utf-8') as f:f.write(encode_content)

支付宝打赏

微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。