爬虫——同步与异步加载

一、同步加载

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

比如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)

运行结果:

相关推荐
菜鸡中的奋斗鸡→挣扎鸡1 小时前
初始爬虫11
开发语言·爬虫·python
凡人的AI工具箱2 小时前
15分钟学 Python 第35天 :Python 爬虫入门(一)
开发语言·数据结构·人工智能·后端·爬虫·python
新缸中之脑2 小时前
ScrapeGraphAI 大模型增强的网络爬虫
爬虫
易辰君2 小时前
python爬虫 - 初识爬虫
开发语言·爬虫·python
人生の三重奏14 小时前
爬虫——爬取小音乐网站
爬虫
能摆一天是一天21 小时前
Python 爬虫 根据ID获得UP视频信息
开发语言·爬虫·python·selenium
NPE~2 天前
爬虫入门 & Selenium使用
爬虫·python·selenium·测试工具·xpath
菜鸡中的奋斗鸡→挣扎鸡2 天前
初始爬虫10
爬虫
菜鸡中的奋斗鸡→挣扎鸡2 天前
初始爬虫9
爬虫