Python爬虫中请求头的格式化
1、使用时,将开发者工具抓取的包的Request Headers复制粘贴到代码中的headerStr上。
2、运行代码后,格式后会打印Headers字符串,可以直接放入代码中使用。
实例
import re headerStr = ''' Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9 Accept-Encoding: gzip, deflate Accept-Language: zh-CN,zh;q=0.9 Connection: keep-alive Cookie: ******* Host: paper.people.com.cn Referer: http://paper.people.com.cn/rmrb/html/2021-10/08/nbs.D110000renmrb_01.htm Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 ''' ret = "" for i in headerStr: if i == '\n': i = "',\n'" ret += i ret = re.sub(": ", "': '", ret) print(ret[3: -3])
1、使用collections.Counter函数对列表进行计数,并通过列表推导式过滤出非唯一值,过滤出计数大于1的值。2、Counter是dict的子类,用来计数可哈希对象。是一个集合,元素像字典键一样 ...