python爬虫保存图片和文件

看了一篇博客,是这么保存图片的:

python爬虫保存图片和文件_第1张图片

 照做:

src = "https://i0.hdslb.com/bfs/sycp/creative_img/201912/01b2f081d58e49c708c1d32b7b8c20ed.jpg"
r = requests.get(src)
with open("C://Users//yg//Desktop//bbb.jpg", "wb")as f:  # wb是写二进制
    f.write(r.content)

注意:保存文件的类型,如jpg、png是写在保存路径的文件名中的。 

python爬虫保存图片和文件_第2张图片

保存成功!


下面保存文件:

url1 = "http://exercise.kingname.info/exercise_requests_get.html"
response = requests.get(url1)
source = response.content.decode()
print(source)
with open("C://Users//yg//Desktop//test_1.txt", 'wb') as f: #将爬虫的内容写入文件
    f.write(response.content)

和保存图片类似,只不过文件的类型变成了.txt

python爬虫保存图片和文件_第3张图片

保存成功! 

你可能感兴趣的:(爬虫,python爬虫保存图片,python爬虫保存文件)