我正在编写用于发送请求和获取网站响应以及解析其内容的函数... 但是当我向波斯网站发送请求时,它无法解码它的内容
def gather_links(page_url):
html_string = ''
try:
response = urlopen(page_url)
if 'text/html' in response.getheader('Content-Type'):
html_bytes = response.read()
html_string = html_bytes.decode("utf-8")
except Exception as e:
print(str(e))
显示此错误,例如 https://www.entekhab.ir/ :
'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte
我怎样才能更改解码此类网站的代码?
最佳答案
你应该使用 requests而不是 urllib。
import requests
response = requests.get('https://www.entekhab.ir/')
print(response.text)
关于python - 解码波斯网站获取请求的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58130647/