小编xin*_*hen的帖子

如何从URL中删除查询字符串?

我有以下网址:

https://stackoverflow.com/questions/7990301?aaa=aaa
https://stackoverflow.com/questions/7990300?fr=aladdin
https://stackoverflow.com/questions/22375#6
https://stackoverflow.com/questions/22375?
https://stackoverflow.com/questions/22375#3_1
Run Code Online (Sandbox Code Playgroud)

我需要网址,例如:

https://stackoverflow.com/questions/7990301
https://stackoverflow.com/questions/7990300
https://stackoverflow.com/questions/22375
https://stackoverflow.com/questions/22375
https://stackoverflow.com/questions/22375
Run Code Online (Sandbox Code Playgroud)

我的尝试:

url='https://stackoverflow.com/questions/7990301?aaa=aaa'
if '?' in url:
    url=url.split('?')[0]
if '#' in url:
    url = url.split('#')[0]
Run Code Online (Sandbox Code Playgroud)

我认为这是一种愚蠢的方式

python url python-2.x python-3.x

6
推荐指数
2
解决办法
1183
查看次数

python3.8 brotli brotli.error:BrotliDecompress失败?

我的代码python3.8

布罗特利=1.0.9

使用请求获取url,标头使用Accept-Encoding =“br”我需要使用解码br,因为我认为使用accept-encoding = br更好

import brotli
import requests 
headers = {}
headers['Accept'] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
headers['Accept-Encoding'] = "gzip, deflate, br"
headers['Host'] = "book.douban.com"
headers['Referer'] = "book.douban.com"
headers['Sec-Fetch-Dest'] = "document"
headers['Sec-Fetch-Mode'] = "navigate"
headers['Upgrade-Insecure-Requests'] = "1"

s=requests.Session()
url="https://book.douban.com/tag/%E5%B0%8F%E8%AF%B4"
try:
    response = s.get(url, headers=headers)
except:
    return ""
if response.status_code == 200:
    print(response.headers)
    if response.headers.get('Content-Encoding') == 'br':
        data = brotli.decompress(response.content)
        data1 = data.decode('utf-8')
        return data1
    else:
        return response.text
return ""
Run Code Online (Sandbox Code Playgroud)

引发错误

data = brotli.decompress(response.content)
brotli.error: BrotliDecompress failed
Run Code Online (Sandbox Code Playgroud)

python-3.x brotli python-3.8

6
推荐指数
1
解决办法
5248
查看次数

pythonic方式处理DataError:类型无效的输入:'dict'。先转换为字节、字符串、整数或浮点数。?

redis 版本 3.4.1 必须使用 hash,不能使用 str 或其他数据类型数据:

{'_anno': {
    'ctp': 'list',
    'dt': [],
    'ml': 0,
    'na': 'apple',
    'pos': -1,
    'rel': '',
    'st_var': '',
    'tp': 'object'},
'_att': {'_cuser': 'apple card',
         '_last_editor': 'apple card',
         '_protext': 'authorize',
         '_status': 'normal',
         '_theme_id': 'apple card',
         '_view': '12'},
    }

Run Code Online (Sandbox Code Playgroud)

我的代码

pool = redis.ConnectionPool(host=host, port=port)
conn = redis.StrictRedis(connection_pool=pool)

conn.hmset("aaaaaa",data)
Run Code Online (Sandbox Code Playgroud)

引发错误

数据错误:类型无效的输入:'dict'。首先转换为字节、字符串、整数或浮点数。

现在代码

pool = redis.ConnectionPool(host=host, port=port)
conn = redis.StrictRedis(connection_pool=pool)
new_data={}
for key,value in data.items():
    new_data[key]=json.dumps(value)
conn.hmset("aaaaaa",new_data)
Run Code Online (Sandbox Code Playgroud)

有没有更pythonic的方式?

redis python-3.x redis-py

5
推荐指数
1
解决办法
5297
查看次数

建立分层评论树?

我正在尝试在我的 django 项目中实现线程注释,我希望它看起来像这样: data

comment_list = [
    {'id': 1, 'content': '...', 'pid': None, 'children_comments': []},
    {'id': 2, 'content': '...', 'pid': None, 'children_comments': []},
    {'id': 3, 'content': '...', 'pid': 1, 'children_comments': []},
    {'id': 4, 'content': '...', 'pid': 3, 'children_comments': []},
    {'id': 5, 'content': '...', 'pid': 4, 'children_comments': []},
    {'id': 6, 'content': '...', 'pid': 2, 'children_comments': []},
    {'id': 7, 'content': '...', 'pid': None, 'children_comments': []},
    {'id': 8, 'content': '...', 'pid': 7, 'children_comments': []},
    {'id': 9, 'content': '...', 'pid': None, 'children_comments': []},
    {'id': …
Run Code Online (Sandbox Code Playgroud)

python

3
推荐指数
1
解决办法
738
查看次数

标签 统计

python-3.x ×3

python ×2

brotli ×1

python-2.x ×1

python-3.8 ×1

redis ×1

redis-py ×1

url ×1