我们网络请求获取图片,如何保存到本地
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import requests url = 'https://t.mwm.moe/fj' #获取风景图 headers = {} for index in range(100): # for循环100次 response = requests.get(url=url,headers=headers) if response.ok: # 保存图片 # 获取二进制数据; 注意: 这里不要解码, 因为图片,视频等文件都是二进制的不是文本,不需要解码 用.content filename = f'''./a{index}.jpg''' with open(filename,'wb') as file_obj: file_obj.write(response.content) else: print('请求失败')
我们网络请求获取文本,如何保存到本地 #知乎的一篇文章地址 为例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 import requests url = 'https://www.zhihu.com/question/22032312/answer/3092917445' #heade头参数 headers = { 'Cookie':'_zap=41862c57-ffa9-4c77-b7a5-4c4b28eefce6; d_c0=AFATn8G_iRaPTg2VAGdO6_h-6-bDZ2WdtAU=|1679981304; q_c1=4891aab9ad2a48e6967c7aa205cb6c55|1679981395000|1679981395000; z_c0=2|1:0|10:1689647957|4:z_c0|80:MS4xUjhaZEZnQUFBQUFtQUFBQVlBSlZUVlZKbzJWSWJCYklWdnlNQk82YnBJUDRIckc5dWRzNkJ3PT0=|a2d8d90aee21c523ca7fcbb2bcec473e05b0a1c719a64cac96c72f1a9044622b; _xsrf=32f990c7-711b-4d99-91af-0f8f38e032d2; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1690514789,1690791979,1690940525,1690973605; tst=r; SESSIONID=6jmBNQRslOsfPrfd8snapX54oUQb9q1hnnjbzCL6w3C; JOID=UlwdAkzZHs8F8lK3ENjdkCOeJqwP63irQbsH6WqyVbBqpCfVeEPkCmDxWrIfsqKngpEVzU2lYKBK2eF-6dhFcek=; osd=Ul8UBE7ZHcYD8FK0Gd7fkCCXIK4P6HGtQ7sE4GywVbNjoiXVe0riCGDyU7QdsqGuhJMVzkSjYqBJ0Od86dtMd-s=; KLBRSID=2177cbf908056c6654e972f5ddc96dc2|1691025315|1691025295; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1691025316', 'Referer':'https://www.zhihu.com/collection/916488495', 'Sec-Ch-Ua':'"Google Chrome";v="113", "Chromium";v="113", "Not-A.Brand";v="24"', 'Sec-Ch-Ua-Mobile':'?0', 'Sec-Ch-Ua-Platform':"Windows", 'Sec-Fetch-Dest':'document', 'Sec-Fetch-Mode':'navigate', 'Sec-Fetch-Site':'same-origin', 'Sec-Fetch-User':'?1', 'Upgrade-Insecure-Requests':'1', 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36' } response = requests.get(url=url,headers=headers) if response.ok: # 设置编码格式 with open("./a.txt",'w',encoding='utf-8') as file: #保存文章内容 file.write(response.text) else: print('请求失败')
python 爬虫中content()和text()的区别 1) text 返回的是unicode 型的数据,一般是在网页的header中定义的编码形式。 2) content返回的是bytes,二进制数据。