爬取12306火车票信息

1. 模拟游览器,防止被发现是爬虫;因为12306的反爬虫做得很严,以常规的爬虫方式无法爬取到所需信息,因此需要模拟成浏览器来对其进行访问,使用一个浏览器打开12306的官方网站,然后从控制台将COOKIE复制出来作为本项目的header

headers = {

    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36",

    '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',

    'Accept-Encoding': 'gzip, deflate, br',

    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',

    'Cache-Control': 'max-age=0',

    'Connection': 'keep-alive',

    'Host': 'kyfw.12306.cn',

    'Referer': 'https://www.12306.cn/',

    'Sec-Fetch-Dest': 'document',

    'Sec-Fetch-Mode': 'navigate',

    'Sec-Fetch-Site': 'same-site',

    'Sec-Fetch-User': '?1',

    'Upgrade-Insecure-Requests': '1',

    "Cookie": "_uab_collina=161396635873945822102

你可能感兴趣的:(python)