'''
cookie反爬
'''
from common.tools import Tool
import requests
from scrapy.selector import Selector
from urllib import parse
class User_Agent:
def __init__(self):
self.tool = Tool.get_tool()
def run(self):
url = 'http://www.porters.vip/verify/cookie/index.html'
headers = {}
headers = self.tool.get_user_agent(headers)
r = requests.get(url=url,headers=headers)
if r.status_code == 200:
cookies = r.headers.get('Set-Cookie')
# cookies = self.tool.get_cookie(cookies)
a = Selector(r).xpath("//a[@class='btn btn-warning']/@href").extract_first()
a = parse.urljoin(r.url,a)
headers['Cookie'] = cookies
response = requests.get(url=a,headers=headers,allow_redirects=False)
print(response.status_code)
print(response.text)
if __name__ == '__main__':
User_Agent().run()
----参考python3反爬虫原理与绕过实战