报错
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()