爬虫——同步与异步加载

一、同步加载

同步模式--阻塞模式(就是会阻止你浏览器的一个后续加载)停止了后续的解析 因此停止了后续的文件加载(图像)

比如hifini音乐网站

二、异步加载

异步加载--xhr(重点)

比如腾讯新闻,腾讯招聘等

三、同步加载和异步加载的区分

1.网页数据返回的方式

(数据返回给你客户端的时候返回的方式有哪些)

---直接返回的网页文本

---ajax加载(通过异步加载回来的数据 一般都是json数据)

----javascript渲染

2.区别

观察你在翻页的时候刷新按钮有没有动

动了 ----- 同步--找数据包优先找all

未动 --异步--找数据包优先找xhr

注意:我们去抓取网站 大致分为两种类别:

---网页文本(html)

-----通过接口返回的数据(json)

爬取腾讯新闻------异步加载

注意:优先找带有list的数据包------offset、limit------headers

点击之后,可以在预览部分查看会否有需要的数据。如果有就说明数据包没有找错。

当你不断往下滑刷新页面后,这时就会出现上面2中,类似的url地址,只不过他的offset会发生变化

示例代码:

import requests
from jsonpath import jsonpath
#发请求
url = "https://i.news.qq.com/trpc.qqnews_web.kv_srv.kv_srv_http_proxy/list"
#ctrl+r
data = {
    'sub_srv_id':'24hours',
    'srv_id':'pc',
    'offset':'40',
    'limit':'20',
    'strategy':'1',
    'ext':'{"pool":["top","hot"],"is_filter":7,"check_type":true}',
}
def get_data():
    headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36'
    }
    r = requests.get(url,headers=headers,params=data)
    if r.status_code==200:
        # d =r.text
        # print(d)
        json_data = r.json()
        # print(json_data)
        return json_data
#解析
def parse_data(data):#形参站位 模拟的就是json_data
    #第一个参数是你要解析的对象 第二个参数是解析语法 $表示根节点 ..表示跳过中间任意层级 直接找到目标层级,.表示一个层级
    title = jsonpath(data,'$..title')#标题
    url = jsonpath(data,'$..url')
    # print(title)
    # print(url)
    for titles,urls in zip(title,url):
        print(titles)
        print(urls)
        print('========================')


if __name__ == '__main__':
    h = get_data()
    parse_data(h)

zip可将多个可迭代对象打包成元组,返回有这些元组组成的列表

四、jsonpath用法

示例代码:

from jsonpath import jsonpath
data = { "store": {
    "book": [
      { "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      { "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      },
      { "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      },
      { "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }
    ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  }
}
authors=jsonpath(data,'$..author')
titles=jsonpath(data,'$.store.book[*].title')
items=jsonpath(data,'$.store.*')
print(authors)
print(titles)
print(items)

运行结果:

相关推荐
A.sir啊13 小时前
爬虫基础(五)爬虫基本原理
网络·爬虫·python·网络协议·http·pycharm
程序员石磊1 天前
学术总结Ai Agent中firecrawl(大模型爬虫平台)的超简单的docker安装方式教程
人工智能·爬虫·docker
_曦1 天前
Scrapy如何设置iP,并实现IP重用, IP代理池重用
爬虫·scrapy
dreadp2 天前
解锁豆瓣高清海报:深度爬虫与requests进阶之路
前端·爬虫·python·beautifulsoup·github·requests
黑不拉几的小白兔3 天前
Python爬虫学习第三弹 —— Xpath 页面解析 & 实现无广百·度
爬虫·python·学习
大懒猫软件3 天前
如何运用python爬虫爬取百度贴吧的静态图片?
爬虫·python·百度
PellyKoo3 天前
Python网络爬虫中的编码乱码如何解决
开发语言·爬虫·python
大懒猫软件4 天前
如何运用python爬虫爬取百度贴吧动态加载的图片?
爬虫·python·dubbo
Jelena157795857925 天前
Python爬虫获取item_search_img-按图搜索淘宝商品(拍立淘)接口
爬虫·python·图搜索算法
whyCoding_5 天前
python爬虫入门(一) - requests库与re库,一个简单的爬虫程序
开发语言·爬虫·python