Zlog
首页时间轴关于
Zlog

一个探索技术、编程和构建 Web 的个人空间。

© 2026 Zlog

导航

首页时间轴关于

链接

首页/Request

Request

Request

Z

Zephyr110

2020年5月19日·1 分钟
|
backendbackend-python
py
import urllib.parse
import urllib.request
import json
 
endpoint = 'https://zhibo.sina.com.cn/api/zhibo/feed'
_params = {
  "page": 1,
  "page_size": 100,
  "zhibo_id": 152,
  "tag_id": 0,
  "dire": 'f',
  "dpc": 1,
  "type": 0
}
 
#把parmas转换为url编码
params = urllib.parse.urlencode(_params)
 
#params注入,拼接url
url = endpoint + '?' + params
 
#如果有header的话,在Request第二个参数注入
# request = urllib.request.Request(url, header)
request = urllib.request.Request(url)
 
#请求结果
response = urllib.request.urlopen(request).read()
 
#读取结果
result_data = json.loads(response)
 
# print(result_data)
# 把数据以json格式写入同目录下的.json文件
try:
    with open('./test.json', 'w', encoding = 'utf8') as json_file:
        json.dump(result_data, json_file, ensure_ascii = False)
    # with 关键字自带close(with是try-finally的简写,但不完全相同)
    # print(json_file.closed)  // True
except ValueError as err:
    print(err)

标签

backendbackend-python

评论

相关文章

Scrapy

或者用 Anaconda(安装略)下载 Scrapy

2021年6月2日·12 分钟

Python常用技巧

⚠️注意,当存在同名健值对,后面的会覆盖掉前面的,因为dict类型不允许有同名键,而且键只能是不可变类型(数字、字符串、元组),列表就不能作为键使用。

2020年5月19日·4 分钟

Decorator(装饰器)

输出小于10000的所有质数,并打印程序执行耗时

2020年4月19日·2 分钟

← 返回文章列表