1、爬虫之请求头中加引号工具
- 在浏览器中复制了请求头的参数(key:value),给参数增加单引号;
- 在getHeadersStr()方法的headers_str变量中粘贴请求头参数;
import re
"""
正则匹配请求头加引号(单引号)
"""
def addSingleQuotation(headers_str):
# 1 2
pattern = r"^(.*?):\s?(.*?$)"
lines = []
for line in headers_str.splitlines():
final_line = re.sub(pattern, r'"\1": "\2",', line)
lines.append(final_line)
# print(final_line)
index = 0
for line in lines[::-1]:
if line.strip() != '':
index = lines.index(line)
break
lines[index] = lines[index].replace(',', '')
for line in lines:
print(line)
# 在这里操作待修改的字符串
def getHeadersStr():
# 在下面输入需要添加引号的数据
headers_str = """
on: true
page: 1
pageSize: 15
productName:
conditionType: 1
applyname:
applysn:
"""
return headers_str
if __name__ == '__main__':
headers_str = getHeadersStr()
addSingleQuotation(headers_str)
输出结果:存放入请求参数param={}字典中
"on": "true",
"page": "1",
"pageSize": "15",
"productName": "",
"conditionType": "1",
"applyname": "",
"applysn": ""