zoukankan      html  css  js  c++  java
  • Python 爬取 ajax 返回的数据

     1 from urllib.request import quote, unquote
     2 import random
     3 import requests
     4 
     5 #quote 将单个字符串编码转化为 %xx 的形式
     6 # strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列
     7 baidu_cat = quote('总榜').strip();
     8 
     9 refer_url = 'https://data.wxb.com/rankArticle'
    10 ajax_url = 'https://data.wxb.com/rank/article?baidu_cat=%s&baidu_tag=&page=1&pageSize=50&type=2&order='%baidu_cat
    11 
    12 headers = {
    13     'Accept': 'application/json',
    14     'Accept-Encoding': 'gzip, deflate, br',
    15     'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
    16     'Connection': 'keep-alive',
    17     'Host': 'data.wxb.com',
    18     'Referer': refer_url,
    19     'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
    20 }
    21 
    22 try:
    23     resp = requests.get(ajax_url, headers=headers)
    24     if resp.status_code == 200:
    25         print(resp.json())  #解析内容为json返回
    26 except requests.ConnectionError as e:
    27     print('Error',e.args) #输出异常信息
    28 
    29 result = resp.json()
    30 for item in result['data']:
    31     print('url:',item['url'])
    32     print('title:',item['title'])
    33 
    34 print("程序结束")
    35 
    36 
    37 
    38 ''' 
    39 pip install requests[security]
    40 
    41 这样会额外的安装如下3个包:
    42 pyOpenSSL
    43 cryptography
    44 idna
    45 
    46 '''
  • 相关阅读:
    [转]scp用法
    进入docker登录psql数据库对特定表进行操作
    [整]swp文件的处理
    shift+zz保存并退出
    [转]python变量作用域的有趣差别
    git 删除分支操作
    混用参数命名方式,确保顺序在命名之前
    [译]Python面试中8个必考问题
    《浪潮之巅》与我的职业选择
    EBS 用户及其联系人的失效时间
  • 原文地址:https://www.cnblogs.com/isungge/p/11598108.html
Copyright © 2011-2022 走看看