前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python爬虫小工具--快速获得请求头

python爬虫小工具--快速获得请求头

作者头像
渔父歌
发布2019-03-01 15:37:44
9780
发布2019-03-01 15:37:44
举报
文章被收录于专栏:数据结构笔记数据结构笔记

我们在写爬虫脚本的时候经常要获取请求头,但是每次从浏览器粘贴到代码里时,都要费一番功夫来处理格式。

于是写了一个请求头转换的脚本,可以将浏览器里复制过来的请求头字符串转换为字典并输出。

代码语言:javascript
复制
import re


def headers_to_dict(headers_str, out_put=True):
    items = headers_str.strip().split('\n')
    headers_dict = {}
    for t in items:
        key, value = re.findall(r'^(\S+):\s*([\s\S]+)$', t)[0]
        headers_dict[key] = value
        if out_put:
            print(f"'{key}': '{value}',")
    return headers_dict

使用说明:

  • headers_str 从浏览器复制的请求头字符串,使用三个单引号
  • out_put 是否输出格式化的字符串,为 True时会将每个键值对以 'key':'value', 的格式输出,可以直接粘贴到字典中。觉得换行字符串不好看的可以用这个将请求头输出,然后手动粘贴到代码中。
  • 返回值,字符串对应的请求头字典。

使用示例:

代码语言:javascript
复制
headers_to_dict(''':authority: www.jianshu.com
:method: GET
:path: /p/b671f67a5960
:scheme: https
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
accept-encoding: gzip, deflate, br
accept-language: zh-CN,zh;q=0.9
cache-control: max-age=0
if-none-match: W/"0d1384f05bc47dfa8d8d26187e1b3f4f"
referer: https://www.jianshu.com/writer
upgrade-insecure-requests: 1
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36''')

#输出
"""
':authority': 'www.jianshu.com',
':method': 'GET',
':path': '/p/b671f67a5960',
':scheme': 'https',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'zh-CN,zh;q=0.9',
'cache-control': 'max-age=0',
'if-none-match': 'W/"0d1384f05bc47dfa8d8d26187e1b3f4f"',
'referer': 'https://www.jianshu.com/writer',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
"""

#返回值
"""
{
    ':authority': 'www.jianshu.com', 
    ':method': 'GET', 
    ':path': '/p/b671f67a5960', 
    ':scheme': 'https', 
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', 
    'accept-encoding': 'gzip, deflate, br', 
    'accept-language': 'zh-CN,zh;q=0.9', 
    'cache-control': 'max-age=0',
    'if-none-match': 'W/"0d1384f05bc47dfa8d8d26187e1b3f4f"', 
    'referer': 'https://www.jianshu.com/writer', 
    'upgrade-insecure-requests': '1', 
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
}
"""
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.01.30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档