入手學習scrapy,簡單學習的官網(wǎng)的demohttps://doc.scrapy.org/en/latest/intro/tutorial.html以后瘫析,試著爬取豆瓣。建立好項目以后硼啤,先用scrapy shell https://movie.douban.com/top250
嘗試质况,結果返回了403,首先想到是要增加user-agent
頭莺丑,于是按照網(wǎng)上的方法分別嘗試
- 在settings.py 里面增加 user_agent 信息著蟹,結果仍然返回403
- 使用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'
}