天天看點

python中requests包的text和content方法的差別

兩者傳回的編碼格式不同。
 
 text傳回的是Unicode編碼,一般是在網頁的header中定義的編碼形式。
 
 而content傳回的是byte類型,二進制資料。
 

 也就是說,如果單單提取文本,那麼用text就可以了。
 
 如果是想要儲存圖檔、檔案等等,需要用到content
 
 例如:
 

 for num, img in enumerate(img_url):
         img_response = requests.get(img)
         image = img_response.content
         image_path = './pictures/%s.png' % num
         fp = open(image_path, 'wb')
         fp.write(image)
         fp.close()