pyspider示例代码六:传递参数
传递参数
示例一
- #!/usr/bin/env python
- # -*- encoding: utf- -*-
- # vim: set et sw= ts= sts= ff=unix fenc=utf8:
- # Created on -- ::
- import re
- import json
- from libs.pprint import pprint
- from libs.base_handler import *
- class Handler(BaseHandler):
- '''
- this is a sample handler
- '''
- crawl_config = {
- }
- proxy = ""
- @every(, )
- def on_start(self):
- self.crawl(self.proxy+'http://www.douban.com/group/haixiuzu/discussion',
- force_update=True, callback=self.index_page)
- @config(age=)
- def index_page(self, response):
- for each in response.doc('tr > .title > a').items():
- self.crawl(self.proxy+each.attr.href, callback=self.detail_page)
- @config(age=***)
- def detail_page(self, response):
- assert response.url != "https://www.douban.com/"
- return {
- "url": response.url,
- "title": response.doc("#content h1").text(),
- "author": response.doc(".topic-content .from a").text(),
- "author_url": response.doc("DIV.topic-doc>H3>SPAN.from>A").attr.href,
- "imgs": [x.attr.src for x in response.doc('.topic-doc img').items()]
- }
- def on_result(self, result):
- if not result or not result['imgs']:
- return
- post_id = re.search("topic/(\d+)", self.response.url).group()
- self.crawl("https://api.duoshuo.com/posts/import.json#"+post_id, method="POST",
- data={
- "short_name": "database",
- "secret": "8e5a5be8873ad7e9a59147c3cfd10e73",
- "posts[0][post_key]": post_id,
- "posts[0][thread_key]": "haixiuzu",
- "posts[0][message]": json.dumps(result).encode("base64").replace("\n", "")
- }, callback=self.post_to_duoshuo)
- def post_to_duoshuo(self):
- pass
示例二
- #!/usr/bin/env python
- # -*- encoding: utf- -*-
- # Created on -- ::
- # Project: prieto
- import re
- from pyspider.libs.base_handler import *
- class Handler(BaseHandler):
- crawl_config = {
- }
- @every(minutes= * )
- def on_start(self):
- for i in range():
- self.crawl('data:,step%d' % i, callback=self.gen_url, save=i)
- @config(priority=)
- def gen_url(self, respond):
- for i in range(respond.save * , (respond.save + ) * ):
- self.crawl("http://bbs.fobshanghai.com/viewthread.php?action=printable&tid=%d" % i, callback=self.index_page)
- @config(priority=)
- def index_page(self, respond):
- # title = response.doc
- hr_black = u'<hr noshade="noshade" size="2" width="100%" color="#808080"/>'
- hr_blue = u'<br/><br/><br/><br/><hr noshade="noshade" size="2" width="100%" color="#698cc3"/>'
- #posts = respond.doc('body').html().split(hr_blue)[].split(hr_black)[:]
- if respond.doc('head').html().startswith('<meta'):
- return {
- "tid": respond.url.split('=')[-],
- "url": respond.url,
- "html": 'The specified thread does not exist.',
- }
- return {
- "tid": respond.url.split('=')[-],
- "url": respond.url,
- #"t_author": posts[].split('\n')[].split('<b>')[].strip(), # 用正则更好
- "html": respond.doc.html(),
- #"replies": [i for i in posts[:]]
- }
pyspider示例代码六:传递参数的更多相关文章
- pyspider示例代码:解析JSON数据
pyspider示例代码官方网站是http://demo.pyspider.org/.上面的示例代码太多,无从下手.因此本人找出一下比较经典的示例进行简单讲解,希望对新手有一些帮助. 示例说明: py ...
- pyspider示例代码三:用PyQuery解析页面数据
本系列文章主要记录和讲解pyspider的示例代码,希望能抛砖引玉.pyspider示例代码官方网站是http://demo.pyspider.org/.上面的示例代码太多,无从下手.因此本人找出一些 ...
- pyspider示例代码二:解析JSON数据
本系列文章主要记录和讲解pyspider的示例代码,希望能抛砖引玉.pyspider示例代码官方网站是http://demo.pyspider.org/.上面的示例代码太多,无从下手.因此本人找出一下 ...
- pyspider示例代码一:利用phantomjs解决js问题
本系列文章主要记录和讲解pyspider的示例代码,希望能抛砖引玉.pyspider示例代码官方网站是http://demo.pyspider.org/.上面的示例代码太多,无从下手.因此本人找出一下 ...
- 【openresty】向lua代码中传递参数
前面介绍FormInputNginxModule模块时,明白了openresty如何获取post提交的数据. 然后,如果需要通过lua处理这些数据,需要把数据作为参数传递到lua中,lua获取了这些数 ...
- pyspider示例代码五:实现自动翻页功能
实现自动翻页功能 示例代码一 #!/usr/bin/env python # -*- encoding: utf- -*- # Created on -- :: # Project: v2ex fro ...
- pyspider示例代码七:自动登陆并获得PDF文件下载地址
自动登陆并获得PDF文件下载地址 #!/usr/bin/env python # -*- encoding: utf- -*- # Created on -- :: # Project: pdf_sp ...
- pyspider示例代码四:搜索引擎爬取
搜索引擎爬取 #!/usr/bin/env python # -*- encoding: utf- -*- # Created on -- :: # Project: __git_lab_fix fr ...
- 传递命令行参数示例代码 (C 和 Python)
C语言 在 C 语言中, 使用 main 函数的输入参数 argc 和 argv 传入命令行参数. argc 为 int 类型, 表示传入命令行参数的个数 (argument count); argv ...
随机推荐
- Sqlserver查询数据库文件大小和剩余空间
在MS Sql Server中可以能过以下的方法查询出磁盘空间的使用情况及各数据库数据文件及日志文件的大小及使用利用率: 1.查询各个磁盘分区的剩余空间:Exec master.dbo.xp_fixe ...
- maven项目--Eclipse报错:java.lang.ClassNotFoundException: ContextLoaderListener
转自:https://yq.aliyun.com/ziliao/597445 Eclipse中tomcat部署工程启动后报错: 二月 25, 2016 2:34:00 下午 org.apache.to ...
- angular controller 之间的通信方式
AngularJS中的controller是个函数,用来向视图的作用域($scope)添加额外的功能,我们用它来给作用域对象设置初始状态,并添加自定义行为. 当我们在创建新的控制器时,angularJ ...
- HTML5拖动
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- delphi ios grid BindSourceDB bug
BindSourceDB4.DataSet :=nil; BindSourceDB4.DataSet :=FDMemTable1; grid绑定后显示数据正常,第二次赋值BindSourceDB4.D ...
- Spring Boot实践——Mybatis分页插件PageHelper的使用
出自:https://blog.csdn.net/csdn_huzeliang/article/details/79350425 在springboot中使用PageHelper插件有两种较为相似的方 ...
- Objective C, post 请求
-(void)postData { NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithS ...
- SQL Server 2008用'sa'登录失败,启用'sa'登录的办法
首先”为什么用sa登录不了,提示登录失败呢?" 当然,自己装SQL Server 2008的时候根本就没有用sa登录的方法,装数据库的时候是用windows身份登录的. 如果要启用用户名为“ ...
- 吴裕雄 数据挖掘与分析案例实战(8)——Logistic回归分类模型
import numpy as npimport pandas as pdimport matplotlib.pyplot as plt # 自定义绘制ks曲线的函数def plot_ks(y_tes ...
- SpringCloud之Eureka 服务注册和服务发现基础篇2
上篇文章讲解了SpringCloud组件和概念介绍,接下来讲解一下SpringCloud组件相关组件使用.原理和每个组件的作用的,它主要提供的模块包括:服务发现(Eureka),断路器(Hystrix ...