深度好文:

from scrapy.contrib.spiders.init import InitSpider
from scrapy.http import Request, FormRequest
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.contrib.spiders import Rule class MySpider(InitSpider):
name = 'myspider'
allowed_domains = ['domain.com']
login_page = 'http://www.domain.com/login'
start_urls = ['http://www.domain.com/useful_page/',
'http://www.domain.com/another_useful_page/'] rules = (
Rule(SgmlLinkExtractor(allow=r'-\w+.html$'),
callback='parse_item', follow=True),
) def init_request(self):
"""This function is called before crawling starts."""
return Request(url=self.login_page, callback=self.login) def login(self, response):
"""Generate a login request."""
return FormRequest.from_response(response,
formdata={'name': 'herman', 'password': 'password'},
callback=self.check_login_response) def check_login_response(self, response):
"""Check the response returned by a login request to see if we are
successfully logged in.
"""
if "Hi Herman" in response.body:
self.log("Successfully logged in. Let's start crawling!")
# Now the crawling can begin..
self.initialized()
else:
self.log("Bad times :(")
# Something went wrong, we couldn't log in, so nothing happens. def parse_item(self, response): # Scrape data from page
备注: 该代码片段来自于: http://www.sharejs.com/codes/python/8544

使用header
request_headers = { 'User-Agent': 'PeekABoo/1.3.7' }
request = urllib2.Request('http://sebsauvage.net', None, request_headers)
urlfile = urllib2.urlopen(request)

scrapy 模拟登录后再抓取的更多相关文章

  1. Scrapy 模拟登陆知乎--抓取热点话题

    工具准备 在开始之前,请确保 scrpay 正确安装,手头有一款简洁而强大的浏览器, 若是你有使用 postman 那就更好了.           Python   1 scrapy genspid ...

  2. scrapy模拟登录微博

    http://blog.csdn.net/pipisorry/article/details/47008981 这篇文章是介绍使用scrapy模拟登录微博,并爬取微博相关内容.关于登录流程为嘛如此设置 ...

  3. Scrapy 通过登录的方式爬取豆瓣影评数据

    Scrapy 通过登录的方式爬取豆瓣影评数据 爬虫 Scrapy 豆瓣 Fly 由于需要爬取影评数据在来做分析,就选择了豆瓣影评来抓取数据,工具使用的是Scrapy工具来实现.scrapy工具使用起来 ...

  4. Charles 安装证书后依旧抓取不到https请求的解决方案

    1.打开charles——>help——>SSL proxying——>Install Charles Root Certificate 证书安装后,抓取https的包 2.查看Pr ...

  5. Python手动构造Cookie模拟登录后获取网站页面内容

    最近有个好友让我帮忙爬取个小说,这个小说是前三十章直接可读,后面章节需要充值VIP可见.所以就需要利用VIP账户登录后,构造Cookie,再用Python的获取每章节的url,得到内容后再使用 PyQ ...

  6. python requests 模拟登陆网站,抓取数据

    抓取页面数据的时候,有时候我们需要登陆才可以获取页面资源,那么我们需要登陆以后才可以跳转到对应的资源页面,那么我们需要通过模拟登陆,登陆成功以后再次去抓取对应的数据. 首先我们需要通过手动方式来登陆一 ...

  7. python爬虫之scrapy模拟登录

    背景: 初来乍到的pythoner,刚开始的时候觉得所有的网站无非就是分析HTML.json数据,但是忽略了很多的一个问题,有很多的网站为了反爬虫,除了需要高可用代理IP地址池外,还需要登录.例如知乎 ...

  8. C#模拟登录后请求查询

    需求是这样子的,想开发一个外挂程序,能够抓取别的系统的数据,从而实现数据验证. 比如这样一个界面: 使用Chrome浏览器分析http请求和响应过程以及页面的html代码,发现这是一个ajax请求,于 ...

  9. scrapy下载中间件结合selenium抓取全国空气质量检测数据

    1.所需知识补充 1.下载中间件常用函数 process_request(self, request, spider): 当每个request通过下载中间件是,该方法被调用 process_reque ...

随机推荐

  1. Linux-SSL和SSH和OpenSSH,OpenSSL有什么区别

    ssl是通讯链路的附加层.可以包含很多协议.https, ftps, ..... ssh只是加密的shell,最初是用来替代telnet的.通过port forward,也可以让其他协议通过ssh的隧 ...

  2. 怎么保护PDF文档和扫描文件里的机密信息

    从事商务工作的人,必然要处理带有机密信息的文档,需要分享这些文档的时候,如何谨慎小心地对待那些机密信息,说到底还是取决于自己.分享文档的目的不同,对文档的保护类型和级别也不一样.例如,只有授权的读者才 ...

  3. Excel 导入并导出结果集

    1.controler代码: /// <summary> /// 导入预归类意见书 /// </summary> /// <param name="upload ...

  4. commonJS规范基本机构

    commonJS规范:使用 module.exports 和 require ,基本结构如下: // foo.js 输出模块 module.exports = function(x) { consol ...

  5. linux下安装svn

    linux下SVN服务器如何搭建和使用 | 浏览:12117 | 更新:2013-09-18 14:28 | 标签:linux linux下SVN服务器如何搭建和使用?说到SVN服务器,想必大家都知道 ...

  6. WAS维护常用操作

    0.WAS学习地址: http://www.open-open.com/doc/list/276?pn=1http://www.doc88.com/p-7498799200332.htmlhttp:/ ...

  7. redis 服务访问密码设定

    1. 更改redis.conf配置 # requirepass foobared 去掉注释,foobared改为 自己的password , 我测试的时候用的是 redis-password 2.启动 ...

  8. java读取properties文件的内容

    获得properties文件中某个key对应着的value // 路径名称 + properties的名称,不要“properties” private static final String BUN ...

  9. IOS开发—数据库的使用

    1.首先封装方法(建一个DataBase类封装) 2.实现DataBase类的方法 3.第一步先建立一个表 4.进行插入.删除.更新 插入的sql语句:@"insert into user ...

  10. Visual Studio Professional 2015 (x86 and x64) - DVD (Chinese-Simplified)

    文件名cn_visual_studio_professional_2015_x86_x64_dvd_6846645.isoSHA1629E7154E2695F08A3C692C0B3F6CE19DF6 ...