我有以下网址:
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)
我认为这是一种愚蠢的方式
我的代码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) 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的方式?
我正在尝试在我的 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)