Discuss / Python / 用了评论区提供的url

用了评论区提供的url

Topic source

alienation

#1 Created at ... [Delete] [Delete and Lock User]

虽然内容为headers,但是是从网页提取的json对象,而不是返回的请求头

更改了判定条件

#利用urllib读取JSON,然后将JSON解析为Python对象:

# -*- coding: utf-8 -*-
from urllib import request
import json
def fetch_data(url):
    req = request.Request(url)
    req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36')
    with request.urlopen(req) as f:
        data = f.read().decode('utf-8')
        d = json.loads(data)
    return d

# 测试
URL = 'http://www.httpbin.org/get'
data = fetch_data(URL)
print(data)
assert data['origin'] == '125.75.162.164'
print('ok')

alienation

#2 Created at ... [Delete] [Delete and Lock User]

由于json没有单独的对象,所以从网页上爬到的json数据在python内为str对象,要将其转变为dict对象,使用json.loads(),该方法可将符合json格式的字符串转换为dict对象

即只要str格式符合json数据格式,就可以使用json.loads()将其字典化

反之用json.dumps()将dict字符串化

4ever

#3 Created at ... [Delete] [Delete and Lock User]

拨云见雾


  • 1

Reply