Python爬虫——Urllib库-3

目录

ajax的get请求

获取豆瓣电影第一页的数据并保存到本地

获取豆瓣电影前十页的数据

ajax的post请求

总结


ajax的get请求

获取豆瓣电影第一页的数据并保存到本地

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

那么我们的url就可以在header中找到了

再加上UA这个header

进行请求对象的定制,模拟浏览器发送请求即可

详细代码如下:

python 复制代码
# 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

然后就可以写出下面的代码了:

python 复制代码
# 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的post请求

对肯德基官网的餐厅位置进行爬取

这为什么是一个ajax发送的数据呢,因为这里有一个ajax的核心对象

然后就通过URL和header就可以得到下面的代码,并没有新的东西 ,都是前面的知识点的整合。

python 复制代码
# 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~

相关推荐
李松桃1 小时前
Python爬虫-实战
爬虫·python
跨境数据猎手2 小时前
B 站 item_search_video 接口开发,搭建生产级视频搜索服务
大数据·爬虫·python
小白学大数据13 小时前
Python 自动化爬取网易云音乐歌手歌词实战教程
爬虫·python·okhttp·自动化
深蓝电商API13 小时前
京东API批量操作优化:单次1000条限制的突破方案
爬虫·接口·api·京东api
Python大数据分析@1 天前
浏览器自动化工具 Selenium,Playwright,Puppeteer 做爬虫有哪些弊病?
爬虫·selenium·自动化
剑神一笑1 天前
从零开始理解 robots.txt:搜索引擎爬虫的“门禁系统“
爬虫·搜索引擎
捉鸭子2 天前
某音a_bogus vmp逆向
爬虫·python·web安全·node.js·js
Python大数据分析@3 天前
CLI一键采集,使用Python搭建TikTok电商爬虫Agent
开发语言·爬虫·python
编程隐士3 天前
爬虫管理系统实现方案
爬虫
跨境数据猎手3 天前
1688 商品铺货到独立站实操(附工具 + 代码)
大数据·爬虫·软件构建