Python爬虫成功解决TypeError: a bytes-like object is required, not ‘str‘

报错

TypeError: a bytes-like object is required, not 'str'

源代码

import requests
response = requests.get('https://localprod.pandateacher.com/python-manuscript/crawler-html/exercise/HTTP%E5%93%8D%E5%BA%94%E7%8A%B6%E6%80%81%E7%A0%81.md')
print(type(response))
print(response.status_code)
print(response.text)
file=open('text','wb')
file.write(response.text)
file.close()

解决方案

file.write(response.text)出错,应该改为.content

import requests
response = requests.get('https://localprod.pandateacher.com/python-manuscript/crawler-html/exercise/HTTP%E5%93%8D%E5%BA%94%E7%8A%B6%E6%80%81%E7%A0%81.md')
print(type(response))
print(response.status_code)
print(response.text)
file=open('text','wb')
file.write(response.content)
file.close()

进一步改进可以使用响应状态码来确认是否爬取成功:

import requests
response = requests.get('https://localprod.pandateacher.com/python-manuscript/crawler-html/exercise/HTTP%E5%93%8D%E5%BA%94%E7%8A%B6%E6%80%81%E7%A0%81.md')
print(type(response))
print(response.status_code)
print(response.text)
if response.status_code == 200:
    file=open('text','wb')
    file.write(response.content)
    file.close()

猜你喜欢

转载自blog.csdn.net/weixin_44991673/article/details/111015723