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~

相关推荐
K哥爬虫4 小时前
【验证码逆向专栏】某奇艺滑块验证码逆向分析
爬虫
K哥爬虫4 小时前
【验证码逆向专栏】某手滑块逆向、风控分析
爬虫
一枚小小程序员哈6 小时前
大数据、hadoop、爬虫、spark项目开发设计之基于数据挖掘的交通流量分析研究
大数据·hadoop·爬虫
君万10 小时前
【go语言】字符串函数
爬虫·python·golang
麦麦大数据1 天前
求职推荐大数据可视化平台招聘系统 Vue+Flask python爬虫 前后端分离
vue.js·爬虫·python·信息可视化·flask·推荐算法·协同过滤
杨荧5 天前
基于Python的宠物服务管理系统 Python+Django+Vue.js
大数据·前端·vue.js·爬虫·python·信息可视化
上海云盾第一敬业销售6 天前
小程序被爬虫攻击,使用waf能防护吗?
爬虫·小程序
小小码农一只6 天前
Python 爬虫实战:玩转 Playwright 跨浏览器自动化(Chromium/Firefox/WebKit 全支持)
爬虫·python·自动化
weixin_443353317 天前
小红书帖子评论的nodejs爬虫脚本
前端·爬虫
TLuoQiu8 天前
小电视视频内容获取GUI工具
爬虫·python