Fake a cookie to scrape a site in python

拜拜、爱过 提交于 2020-01-11 07:04:09

问题


The site that I'm trying to scrape uses js to create a cookie. What I was thinking was that I can create a cookie in python and then use that cookie to scrape the site. However, I don't know any way of doing that. Does anybody have any ideas?


回答1:


Please see Python httplib2 - Handling Cookies in HTTP Form Posts for an example of adding a cookie to a request.

I often need to automate tasks in web based applications. I like to do this at the protocol level by simulating a real user's interactions via HTTP. Python comes with two built-in modules for this: urllib (higher level Web interface) and httplib (lower level HTTP interface).




回答2:


If you want to do more involved browser emulation (including setting cookies) take a look at mechanize. It's simulation capabilities are almost complete (no Javascript support unfortunately): I've used it to build several scrapers with much success.



来源:https://stackoverflow.com/questions/1117491/fake-a-cookie-to-scrape-a-site-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!