目录
首先可以在浏览器找到发送数据的接口

那么我们的url就可以在header中找到了
再加上UA这个header
进行请求对象的定制,模拟浏览器发送请求即可
详细代码如下:
- # get请求
- # 获取豆瓣电影第一页的数据并且保存起来
- import urllib.request
-
- url = 'https://movie.douban.com/j/chart/top_list?type=13&interval_id=100%3A90&action=&start=0&limit=20'
-
- 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(url=url, headers=headers)
-
- # 模拟浏览器发送请求,获取响应的数据
- response = urllib.request.urlopen(request)
- content = response.read().decode('utf-8')
- # print(content)
-
- # 将数据下载到本地
- # open方法默认使用GBK,但是我们前面使用的是utf-8,那么这里
- # 需要将编码格式指定为utf-8
- fp = open('douban.json', 'w', encoding='utf-8')
- fp.write(content)
-
- # get请求
- # 获取豆瓣电影第一页的数据并且保存起来
- import urllib.request
-
- url = 'https://movie.douban.com/j/chart/top_list?type=13&interval_id=100%3A90&action=&start=0&limit=20'
-
- 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(url=url, headers=headers)
-
- # 模拟浏览器发送请求,获取响应的数据
- response = urllib.request.urlopen(request)
- content = response.read().decode('utf-8')
- # print(content)
-
- # 将数据下载到本地
- # open方法默认使用GBK,但是我们前面使用的是utf-8,那么这里
- # 需要将编码格式指定为utf-8
- fp = open('douban.json', 'w', encoding='utf-8')
- fp.write(content)
-
这就下载下来了

首先我们找到第一次的刷新数据的请求url:
https://movie.douban.com/j/chart/top_list?type=13&interval_id=100%3A90&action=&start=0&limit=20
然后是第二次的:
https://movie.douban.com/j/chart/top_list?type=13&interval_id=100%3A90&action=&start=20&limit=20
然后是第三次的:
https://movie.douban.com/j/chart/top_list?type=13&interval_id=100%3A90&action=&start=40&limit=20
如果你观察这几个URL后面的参数的话,你就可以发现问题了,start每次都累加上limit,通过改变起始索引来挨个查询,这个在Java开发中经常会有这种代码,那么它查询的方法就已经是显而易见了。
所以可以得出start的值是:(page - 1) * 20
然后就可以写出下面的代码了:
- # get请求
- # 下载豆瓣电影前十页的数据
- import urllib.request
- import urllib.parse
-
- """
- 得到不同pages的request
- """
- def create_request(page):
- base_url = 'https://movie.douban.com/j/chart/top_list?type=13&interval_id=100%3A90&action=&'
-
- data = {
- 'start': (page - 1) * 20,
- 'limit': 20
- }
- data = urllib.parse.urlencode(data)
- url = base_url + data
- print(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(url=url, headers=headers)
- return request
-
- """
- 得到返回的内容content
- """
- def 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', encoding='utf-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发送的数据呢,因为这里有一个ajax的核心对象

然后就通过URL和header就可以得到下面的代码,并没有新的东西 ,都是前面的知识点的整合。
- # post请求
- # 肯德基官网
- import urllib.request
- import urllib.parse
-
- # 第一页
- # https://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname
-
- # cname: 哈尔滨
- # pid:
- # pageIndex: 1
- # pageSize: 10
-
- # 第二页
- # https://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname
-
- # cname: 哈尔滨
- # pid:
- # pageIndex: 2
- # pageSize: 10
-
- """
- 请求对象定制
- """
- def create_request(page):
- base_url = 'https://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname'
-
- data = {
- '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(url=base_url, headers=headers, data=data)
- 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', encoding='utf-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~