python爬取猫眼票房的数据,遇到美团登录验证怎么办

python爬取猫眼票房的数据
地址:https://piaofang.maoyan.com/?ver=normal

import requests
from fake_useragent import UserAgent
import base64
from lxml import etree
from requests import RequestException


# fake_useragent使用
ua = UserAgent()


def download_page(url):
    headers = {
        'User-Agent': ua.random,
        'Sec - Fetch - Mode': 'no-cors',
        'Referer': 'https://piaofang.maoyan.com/?ver=normal'
    }

    try:
        response = requests.get(url=url, headers=headers)  

        if response.status_code == 200:
            return response.content
        else:
            print(response.status_code)
            return None

    except RequestException:
        print(RequestException.args)
        return None


def get_contents(html):
    print(html)


def main():
    
    url = 'https://piaofang.maoyan.com/?ver=normal'
    html = download_page(url)
    get_contents(html)



if __name__ == '__main__':
    main()

使用requests.get(url=url, headers=headers)爬取到的网页是美团登录验证的网页,请问怎样才能继续爬取数据

接触爬虫时间不久,所以很多知识不足,请各位大佬指教,提供具体思路就好

谢谢大家,祝大家生活开心

阅读 6.9k
2 个回答

既然是刚学不久,那么很简单,去登录一下,然后把 cookie 粘贴到你的 requests 的 header 里,然后你去访问的时候,就相当于是已经登录了,但是这个失效就不一定是多久了。

这种是非常简单的一个绕过措施,很有可能你爬到第 N 页,他们页面还有一个验证码要你输入,那就不是 requests 能够解决的了,这个等你爬虫学的再深入一些,你自己就知道该怎么做了。

爬虫时,先访问登陆页,用账户密码登录,登陆后携cookie在去request这个页面

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题