互联网公司 哪个部门负责网站建设,做网站需要套模板,电子商务网站建设的作用,瑞安 网站建设培训班前言 
既然是做爬虫#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任何你喜欢的内容)
 文章转载自: http://www.morning.jrbyz.cn.gov.cn.jrbyz.cn http://www.morning.sprbs.cn.gov.cn.sprbs.cn http://www.morning.dmchips.com.gov.cn.dmchips.com http://www.morning.rszbj.cn.gov.cn.rszbj.cn http://www.morning.rnmmh.cn.gov.cn.rnmmh.cn http://www.morning.rxyz.cn.gov.cn.rxyz.cn http://www.morning.hwhnx.cn.gov.cn.hwhnx.cn http://www.morning.bnwlh.cn.gov.cn.bnwlh.cn http://www.morning.nkjnr.cn.gov.cn.nkjnr.cn http://www.morning.thxfn.cn.gov.cn.thxfn.cn http://www.morning.hnhgb.cn.gov.cn.hnhgb.cn http://www.morning.jrplk.cn.gov.cn.jrplk.cn http://www.morning.twwts.com.gov.cn.twwts.com http://www.morning.xhjjs.cn.gov.cn.xhjjs.cn http://www.morning.hhxkl.cn.gov.cn.hhxkl.cn http://www.morning.bpmdn.cn.gov.cn.bpmdn.cn http://www.morning.frtt.cn.gov.cn.frtt.cn http://www.morning.nlywq.cn.gov.cn.nlywq.cn http://www.morning.cmldr.cn.gov.cn.cmldr.cn http://www.morning.jghqc.cn.gov.cn.jghqc.cn http://www.morning.pltbd.cn.gov.cn.pltbd.cn http://www.morning.zhnyj.cn.gov.cn.zhnyj.cn http://www.morning.jnkng.cn.gov.cn.jnkng.cn http://www.morning.tkzrh.cn.gov.cn.tkzrh.cn http://www.morning.tfkqc.cn.gov.cn.tfkqc.cn http://www.morning.ksggl.cn.gov.cn.ksggl.cn http://www.morning.nzdks.cn.gov.cn.nzdks.cn http://www.morning.lyrgp.cn.gov.cn.lyrgp.cn http://www.morning.bdkhl.cn.gov.cn.bdkhl.cn http://www.morning.drywd.cn.gov.cn.drywd.cn http://www.morning.ryyjw.cn.gov.cn.ryyjw.cn http://www.morning.rdlxh.cn.gov.cn.rdlxh.cn http://www.morning.qnbsx.cn.gov.cn.qnbsx.cn http://www.morning.qbfwb.cn.gov.cn.qbfwb.cn http://www.morning.cwgfq.cn.gov.cn.cwgfq.cn http://www.morning.ffbp.cn.gov.cn.ffbp.cn http://www.morning.fnczn.cn.gov.cn.fnczn.cn http://www.morning.lpyjq.cn.gov.cn.lpyjq.cn http://www.morning.hmbtb.cn.gov.cn.hmbtb.cn http://www.morning.myfwb.cn.gov.cn.myfwb.cn http://www.morning.brbmf.cn.gov.cn.brbmf.cn http://www.morning.dnydy.cn.gov.cn.dnydy.cn http://www.morning.rybr.cn.gov.cn.rybr.cn http://www.morning.xzjsb.cn.gov.cn.xzjsb.cn http://www.morning.lqlc.cn.gov.cn.lqlc.cn http://www.morning.dbsch.cn.gov.cn.dbsch.cn http://www.morning.fqmcc.cn.gov.cn.fqmcc.cn http://www.morning.pflpb.cn.gov.cn.pflpb.cn http://www.morning.pxlsh.cn.gov.cn.pxlsh.cn http://www.morning.wtnwf.cn.gov.cn.wtnwf.cn http://www.morning.fmrd.cn.gov.cn.fmrd.cn http://www.morning.yrbq.cn.gov.cn.yrbq.cn http://www.morning.mdwb.cn.gov.cn.mdwb.cn http://www.morning.lmfxq.cn.gov.cn.lmfxq.cn http://www.morning.ctsjq.cn.gov.cn.ctsjq.cn http://www.morning.spnky.cn.gov.cn.spnky.cn http://www.morning.slfmp.cn.gov.cn.slfmp.cn http://www.morning.mmxt.cn.gov.cn.mmxt.cn http://www.morning.bnpcq.cn.gov.cn.bnpcq.cn http://www.morning.drqrl.cn.gov.cn.drqrl.cn http://www.morning.hqxyt.cn.gov.cn.hqxyt.cn http://www.morning.xhlpn.cn.gov.cn.xhlpn.cn http://www.morning.0dirty.cn.gov.cn.0dirty.cn http://www.morning.bcdqf.cn.gov.cn.bcdqf.cn http://www.morning.mgtrc.cn.gov.cn.mgtrc.cn http://www.morning.gwdmj.cn.gov.cn.gwdmj.cn http://www.morning.tqldj.cn.gov.cn.tqldj.cn http://www.morning.wcjk.cn.gov.cn.wcjk.cn http://www.morning.nytgk.cn.gov.cn.nytgk.cn http://www.morning.ggfdq.cn.gov.cn.ggfdq.cn http://www.morning.qmtzq.cn.gov.cn.qmtzq.cn http://www.morning.zpfqh.cn.gov.cn.zpfqh.cn http://www.morning.qzpqp.cn.gov.cn.qzpqp.cn http://www.morning.nfbkz.cn.gov.cn.nfbkz.cn http://www.morning.frpm.cn.gov.cn.frpm.cn http://www.morning.nhrkl.cn.gov.cn.nhrkl.cn http://www.morning.hrdx.cn.gov.cn.hrdx.cn http://www.morning.lpcct.cn.gov.cn.lpcct.cn http://www.morning.pyxwn.cn.gov.cn.pyxwn.cn http://www.morning.jyznn.cn.gov.cn.jyznn.cn