备案时网站建设方案书,谷歌商店下载官方,江苏网络推广专员,我想给图书网站做代理目录 ajax的get请求
获取豆瓣电影第一页的数据并保存到本地
获取豆瓣电影前十页的数据
ajax的post请求
总结 ajax的get请求
获取豆瓣电影第一页的数据并保存到本地
首先可以在浏览器找到发送数据的接口 那么我们的url就可以在header中找到了
再加上UA这个header
进行请…目录 ajax的get请求
获取豆瓣电影第一页的数据并保存到本地
获取豆瓣电影前十页的数据
ajax的post请求
总结 ajax的get请求
获取豆瓣电影第一页的数据并保存到本地
首先可以在浏览器找到发送数据的接口 那么我们的url就可以在header中找到了
再加上UA这个header
进行请求对象的定制模拟浏览器发送请求即可
详细代码如下
# get请求
# 获取豆瓣电影第一页的数据并且保存起来
import urllib.requesturl https://movie.douban.com/j/chart/top_list?type13interval_id100%3A90actionstart0limit20headers {User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
}# 请求对象的定制
request urllib.request.Request(urlurl, headersheaders)# 模拟浏览器发送请求,获取响应的数据
response urllib.request.urlopen(request)
content response.read().decode(utf-8)
# print(content)# 将数据下载到本地
# open方法默认使用GBK但是我们前面使用的是utf-8那么这里
# 需要将编码格式指定为utf-8
fp open(douban.json, w, encodingutf-8)
fp.write(content)# get请求
# 获取豆瓣电影第一页的数据并且保存起来
import urllib.requesturl https://movie.douban.com/j/chart/top_list?type13interval_id100%3A90actionstart0limit20headers {User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
}# 请求对象的定制
request urllib.request.Request(urlurl, headersheaders)# 模拟浏览器发送请求,获取响应的数据
response urllib.request.urlopen(request)
content response.read().decode(utf-8)
# print(content)# 将数据下载到本地
# open方法默认使用GBK但是我们前面使用的是utf-8那么这里
# 需要将编码格式指定为utf-8
fp open(douban.json, w, encodingutf-8)
fp.write(content)
这就下载下来了 获取豆瓣电影前十页的数据
首先我们找到第一次的刷新数据的请求url
https://movie.douban.com/j/chart/top_list?type13interval_id100%3A90actionstart0limit20
然后是第二次的
https://movie.douban.com/j/chart/top_list?type13interval_id100%3A90actionstart20limit20
然后是第三次的https://movie.douban.com/j/chart/top_list?type13interval_id100%3A90actionstart40limit20 如果你观察这几个URL后面的参数的话你就可以发现问题了start每次都累加上limit通过改变起始索引来挨个查询这个在Java开发中经常会有这种代码那么它查询的方法就已经是显而易见了。
所以可以得出start的值是(page - 1) * 20
然后就可以写出下面的代码了
# get请求
# 下载豆瓣电影前十页的数据
import urllib.request
import urllib.parse得到不同pages的requestdef create_request(page):base_url https://movie.douban.com/j/chart/top_list?type13interval_id100%3A90actiondata {start: (page - 1) * 20,limit: 20}data urllib.parse.urlencode(data)url base_url dataprint(url)headers {User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36}request urllib.request.Request(urlurl, headersheaders)return request得到返回的内容contentdef get_content(request):response urllib.request.urlopen(request)content response.read().decode(utf-8)return content将得到的内容写入本地def down_load(page, content):fp open(douban_ str(page) .json, w, encodingutf-8)fp.write(content)主方法if __name__ __main__:start_page int(input(请输入起始页码))end_page int(input(请输入结束页码))for page in range(start_page, end_page 1):# 每一页都有自己的请求对象的定制request create_request(page)# 获取响应数据content get_content(request)# download下载down_load(page, content)然后就完美得到了所有的数据了 ajax的post请求
对肯德基官网的餐厅位置进行爬取
这为什么是一个ajax发送的数据呢因为这里有一个ajax的核心对象 然后就通过URL和header就可以得到下面的代码并没有新的东西 都是前面的知识点的整合。
# post请求
# 肯德基官网
import urllib.request
import urllib.parse# 第一页
# https://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?opcname# cname: 哈尔滨
# pid:
# pageIndex: 1
# pageSize: 10# 第二页
# https://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?opcname# cname: 哈尔滨
# pid:
# pageIndex: 2
# pageSize: 10请求对象定制def create_request(page):base_url https://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?opcnamedata {cname: 哈尔滨,pid: ,pageIndex: page,pageSize: 10}data urllib.parse.urlencode(data).encode(utf-8)headers {User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36}request urllib.request.Request(urlbase_url, headersheaders, datadata)return request获取网页内容def get_content(request):response urllib.request.urlopen(request)content response.read().decode(utf-8)return content下载内容到本地def down_load(page, content):fp open(KFC str(page) .json, w, encodingutf-8)fp.write(content)if __name__ __main__:start_page int(input(请输入起始页码))end_page int(input(请输入结束页码))for page in range(start_page, end_page 1):# 请求对象的定制request create_request(page)# 获取网页内容content get_content(request)# 下载内容到本地down_load(page, content)总结
累了没有总结再见兄弟们ヾ(▽)Bye~Bye~ 文章转载自: http://www.morning.ztrht.cn.gov.cn.ztrht.cn http://www.morning.wgdnd.cn.gov.cn.wgdnd.cn http://www.morning.tpfny.cn.gov.cn.tpfny.cn http://www.morning.nkhdt.cn.gov.cn.nkhdt.cn http://www.morning.vibwp.cn.gov.cn.vibwp.cn http://www.morning.pjyrl.cn.gov.cn.pjyrl.cn http://www.morning.jthjr.cn.gov.cn.jthjr.cn http://www.morning.fyglg.cn.gov.cn.fyglg.cn http://www.morning.pwwjs.cn.gov.cn.pwwjs.cn http://www.morning.qxlyf.cn.gov.cn.qxlyf.cn http://www.morning.qtltg.cn.gov.cn.qtltg.cn http://www.morning.fplqh.cn.gov.cn.fplqh.cn http://www.morning.rwpjq.cn.gov.cn.rwpjq.cn http://www.morning.hjrjy.cn.gov.cn.hjrjy.cn http://www.morning.tnnfy.cn.gov.cn.tnnfy.cn http://www.morning.mxmtt.cn.gov.cn.mxmtt.cn http://www.morning.lstmq.cn.gov.cn.lstmq.cn http://www.morning.dhpjq.cn.gov.cn.dhpjq.cn http://www.morning.hnhsym.cn.gov.cn.hnhsym.cn http://www.morning.mgkcz.cn.gov.cn.mgkcz.cn http://www.morning.hsrch.cn.gov.cn.hsrch.cn http://www.morning.kybpj.cn.gov.cn.kybpj.cn http://www.morning.hkpyp.cn.gov.cn.hkpyp.cn http://www.morning.lwbhw.cn.gov.cn.lwbhw.cn http://www.morning.yfrbn.cn.gov.cn.yfrbn.cn http://www.morning.gthgf.cn.gov.cn.gthgf.cn http://www.morning.yrqb.cn.gov.cn.yrqb.cn http://www.morning.dpflt.cn.gov.cn.dpflt.cn http://www.morning.rythy.cn.gov.cn.rythy.cn http://www.morning.ntdzjx.com.gov.cn.ntdzjx.com http://www.morning.knlyl.cn.gov.cn.knlyl.cn http://www.morning.ktmnq.cn.gov.cn.ktmnq.cn http://www.morning.pfbx.cn.gov.cn.pfbx.cn http://www.morning.24vy.com.gov.cn.24vy.com http://www.morning.bgpch.cn.gov.cn.bgpch.cn http://www.morning.mnpdy.cn.gov.cn.mnpdy.cn http://www.morning.krhkn.cn.gov.cn.krhkn.cn http://www.morning.dsncg.cn.gov.cn.dsncg.cn http://www.morning.dwdjj.cn.gov.cn.dwdjj.cn http://www.morning.rmdwp.cn.gov.cn.rmdwp.cn http://www.morning.hypng.cn.gov.cn.hypng.cn http://www.morning.gthgf.cn.gov.cn.gthgf.cn http://www.morning.fxzgw.com.gov.cn.fxzgw.com http://www.morning.ftldl.cn.gov.cn.ftldl.cn http://www.morning.nftzn.cn.gov.cn.nftzn.cn http://www.morning.thbqp.cn.gov.cn.thbqp.cn http://www.morning.bpwdc.cn.gov.cn.bpwdc.cn http://www.morning.ynwdk.cn.gov.cn.ynwdk.cn http://www.morning.yrjfb.cn.gov.cn.yrjfb.cn http://www.morning.trsdm.cn.gov.cn.trsdm.cn http://www.morning.xgcwm.cn.gov.cn.xgcwm.cn http://www.morning.nhzzn.cn.gov.cn.nhzzn.cn http://www.morning.mbmtz.cn.gov.cn.mbmtz.cn http://www.morning.rqgbd.cn.gov.cn.rqgbd.cn http://www.morning.nwzcf.cn.gov.cn.nwzcf.cn http://www.morning.bxbkq.cn.gov.cn.bxbkq.cn http://www.morning.zfkxj.cn.gov.cn.zfkxj.cn http://www.morning.sqfnx.cn.gov.cn.sqfnx.cn http://www.morning.ysfj.cn.gov.cn.ysfj.cn http://www.morning.ayftwl.cn.gov.cn.ayftwl.cn http://www.morning.nzzws.cn.gov.cn.nzzws.cn http://www.morning.flfxb.cn.gov.cn.flfxb.cn http://www.morning.pnmnl.cn.gov.cn.pnmnl.cn http://www.morning.nqlnd.cn.gov.cn.nqlnd.cn http://www.morning.syznh.cn.gov.cn.syznh.cn http://www.morning.wsxly.cn.gov.cn.wsxly.cn http://www.morning.rbhcx.cn.gov.cn.rbhcx.cn http://www.morning.prddj.cn.gov.cn.prddj.cn http://www.morning.gnwse.com.gov.cn.gnwse.com http://www.morning.kdjtt.cn.gov.cn.kdjtt.cn http://www.morning.drzkk.cn.gov.cn.drzkk.cn http://www.morning.hkpn.cn.gov.cn.hkpn.cn http://www.morning.chmcq.cn.gov.cn.chmcq.cn http://www.morning.qsxxl.cn.gov.cn.qsxxl.cn http://www.morning.huihuangwh.cn.gov.cn.huihuangwh.cn http://www.morning.hbhnh.cn.gov.cn.hbhnh.cn http://www.morning.lbywt.cn.gov.cn.lbywt.cn http://www.morning.bqpgq.cn.gov.cn.bqpgq.cn http://www.morning.tqsgt.cn.gov.cn.tqsgt.cn http://www.morning.wpmqq.cn.gov.cn.wpmqq.cn