做网站需要套模板,51简历模板网,wordpress后台添加友情链接,免费ppt模板网址前言
既然是做爬虫#xff0c;那么肯定就会有一些小心思#xff0c;比如去获取一些自己喜欢的资料等。
去百度图片去抓取图片吧
打开百度图片网站#xff0c;点击搜索xxx#xff0c;打开后#xff0c;滚动滚动条#xff0c;发现滚动条越来越小#xff0c;说明图片加载…前言
既然是做爬虫那么肯定就会有一些小心思比如去获取一些自己喜欢的资料等。
去百度图片去抓取图片吧
打开百度图片网站点击搜索xxx打开后滚动滚动条发现滚动条越来越小说明图片加载是动态的应该是通过ajax获取数据的网站地址栏根本不是真正的图片地址。按F12打开开发者模式我们边滚动边分析发现下面的url才是真正获取图片地址的。
https://image.baidu.com/search/acjson?tnresultjson_comlogidxxxxxxxipnrjct201326592isfpresultfrword你搜索的内容queryWord你搜索的内容cllmieutf-8oeutf-8adpicidstzichdlatestcopyrightssetabwidthheightfaceistypeqcncexpermodenojcisAsyncpn120rn30gsm781721292699879盲目分析分析其中pn120这有可能就是page number这里的pn rn 很有可能就是page_size row_num然后再去试着修改pn值为03060去试试发现果然数据不同得。实锤了… 下面是pn30的数据 太严格了只能发图片了不知道能不能通过 通过分析可知data中就是真正的图片数据好了我们可以拿到url拿到各种数据了。
用python去爬取数据 这要分几个步骤 我们是循环爬取数据的爬取数据后还得保存到文件夹中所以要引入os 以及 requests库 上代码
创建文件夹
# 需要用来创建文件夹
import os
# 在当前目录创建文件夹咱就简单的弄吧别搞复杂的
def mkdir_dir_at_curr_path(dir_name):try:os.mkdir(dir_name)print(文件夹,dir_name,创建成功)except FileExistsError:print(文件夹,dir_name,已经存在)def get_headers():return {User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.114 Safari/537.36,Access-Control-Allow-Credentials: true,Access-Control-Allow-Origin: https://m.baidu.com, https://www.baidu.com, http://m.baidu.com,http://www.baidu.com,Connection:keep-alive,Content-Encoding: br,Content-Type:application/json}定义url以及headers 因为是循环爬取所以url肯定是动态的也就是改一下pn的值查询的人物的名称保证通用性。而且发现单纯的请求返回的数据不正常这个时候我们就得加上headers了这个没办法百度肯定会有一些防御性的措施来防止爬虫捣乱。 # 需要发送请求
import requestsdef get_headers():return {User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.114 Safari/537.36,Access-Control-Allow-Credentials: true,Access-Control-Allow-Origin: https://m.baidu.com, https://www.baidu.com, http://m.baidu.com,http://www.baidu.com,Connection:keep-alive,Content-Encoding: br,Content-Type:application/json}
def get_url(search_name,page_size):urlhttps://image.baidu.com/search/acjson?tnresultjson_comlogid8332766429333445053ipnrjct201326592isfpresultfrwordsearch_namequeryWordsearch_namecl2lmieutf-8oeutf-8adpicidst-1zichdlatestcopyrightssetabwidthheightface0istype2qcnc1expermodenojcisAsyncpnstr(page_size)rn30gsm3c1721294093333return url
下载的主体逻辑
# 定义函数去下载图片
def down_load_pics(search_name):# 创建文件夹mkdir_dir_at_curr_path(search_name)#是否继续循环去下载flagTrue# 确定是第几次下载request_count0while(flag):print(第,request_count1,次下载中)# 获取urldownload_numrequest_count*30url get_url(search_name,download_num)# 获取请求头headersget_headers()#发送请求获得响应数据resprequests.get(url,headersheaders)# 确定是json数据了jsonDataresp.json()if data not in jsonData or jsonData[data][] or jsonData[data][{}]:print(已经全部下载完成)# 下载完了就要跳出循环flagFalsereturn# 有数据就去下载for item in jsonData[data]:if thumbURL in item and fromPageTitleEnc in item and search_name in item[fromPageTitleEnc]:# 图片的真正地址sub_urlitem[thumbURL]if sub_url.startswith(http):responserequests.get(sub_url)# 文件夹中文件数量用来计算下载图片名称file_size len(os.listdir(search_name))# 下载后图片名称下标pic_indexfile_size1#图片名称curr_file_namesearch_name_str(pic_index)# 将下载好的图片数据保存到文件夹中with open(str(search_name/curr_file_name).jpg,wb) as f:f.write(response.content)print(第,pic_index,张图片下载完成)# 准备下一次循环request_count request_count 1最后可以去测试一下了
测试
if __name__ __main__:down_load_pics(你搜索的内容)真的是perfect完全达到预期 现在是不是感觉自己很帅啊哈哈
下面附上完整的代码朋友们记得点个赞哦~~
# 需要发送请求
import requests
# 需要用来创建文件夹
import os# 定义函数去下载图片
def down_load_pics(search_name):# 创建文件夹mkdir_dir_at_curr_path(search_name)#是否继续循环去下载flagTrue# 确定是第几次下载request_count0while(flag):print(第,request_count1,次下载中)# 获取urldownload_numrequest_count*30url get_url(search_name,download_num)# 获取请求头headersget_headers()#发送请求获得响应数据resprequests.get(url,headersheaders)# 确定是json数据了jsonDataresp.json()if data not in jsonData or jsonData[data][] or jsonData[data][{}]:print(已经全部下载完成)# 下载完了就要跳出循环flagFalsereturn# 有数据就去下载for item in jsonData[data]:if thumbURL in item and fromPageTitleEnc in item and search_name in item[fromPageTitleEnc]:# 图片的真正地址sub_urlitem[thumbURL]if sub_url.startswith(http):responserequests.get(sub_url)# 文件夹中文件数量用来计算下载图片名称file_size len(os.listdir(search_name))# 下载后图片名称下标pic_indexfile_size1#图片名称curr_file_namesearch_name_str(pic_index)# 将下载好的图片数据保存到文件夹中with open(str(search_name/curr_file_name).jpg,wb) as f:f.write(response.content)print(第,pic_index,张图片下载完成)# 准备下一次循环request_count request_count 1
def get_headers():return {User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.114 Safari/537.36,Access-Control-Allow-Credentials: true,Access-Control-Allow-Origin: https://m.baidu.com, https://www.baidu.com, http://m.baidu.com,http://www.baidu.com,Connection:keep-alive,Content-Encoding: br,Content-Type:application/json}
def get_url(search_name,page_size):urlhttps://image.baidu.com/search/acjson?tnresultjson_comlogid8332766429333445053ipnrjct201326592isfpresultfrwordsearch_namequeryWordsearch_namecl2lmieutf-8oeutf-8adpicidst-1zichdlatestcopyrightssetabwidthheightface0istype2qcnc1expermodenojcisAsyncpnstr(page_size)rn30gsm3c1721294093333return url
# 在当前目录创建文件夹咱就简单的弄吧别搞复杂的
def mkdir_dir_at_curr_path(dir_name):try:os.mkdir(dir_name)print(文件夹,dir_name,创建成功)except FileExistsError:print(文件夹,dir_name,已经存在)if __name__ __main__:down_load_pics(xxx任何你喜欢的内容)