美文网首页
scrapy爬取豆瓣电影403

scrapy爬取豆瓣电影403

作者: 昵称不再更新 | 来源:发表于2018-09-10 23:59 被阅读0次

    入手学习scrapy,简单学习的官网的demohttps://doc.scrapy.org/en/latest/intro/tutorial.html以后,试着爬取豆瓣。建立好项目以后,先用scrapy shell https://movie.douban.com/top250尝试,结果返回了403,首先想到是要增加user-agent头,于是按照网上的方法分别尝试

    1. 在settings.py 里面增加 user_agent 信息,结果仍然返回403
    2. 使用scrapy shell -s user_agent= xxx url ,结果仍然返回403

    最后尝试在setting中不仅仅增加user_agent信息,添加了以下信息后可以正确访问豆瓣了,基本上就是浏览器中除了cookie的其他信息了吧。

    DEFAULT_REQUEST_HEADERS = {
    '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',
    'Connection': 'keep-alive',
    'Host': 'music.douban.com',
    'Upgrade-Insecure-Requests':' 1',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
    }
    

    相关文章

      网友评论

          本文标题:scrapy爬取豆瓣电影403

          本文链接:https://www.haomeiwen.com/subject/pqfygftx.html