首页 技术 正文
技术 2022年11月7日
0 收藏 376 点赞 558 浏览 2762 个字

实现自动翻页功能

示例代码一

#!/usr/bin/env python
# -*- encoding: utf- -*-
# Created on -- ::
# Project: v2exfrom pyspider.libs.base_handler import *
#import reclass Handler(BaseHandler):
crawl_config = {
} @every(minutes= * )
def on_start(self):
self.crawl('https://www.v2ex.com/', callback=self.index_page, validate_cert=False) @config(age= * * * )
def index_page(self, response):
for each in response.doc('a[href^="https://www.v2ex.com/?tab="]').items():
self.crawl(each.attr.href, callback=self.tab_page, validate_cert=False) @config(age= * * * )
def tab_page(self, response):
for each in response.doc('a[href^="https://www.v2ex.com/go/"]').items():
self.crawl(each.attr.href, callback=self.board_page, validate_cert=False) @config(priority=)
def board_page(self, response):
#实现自动翻页功能
for each in response.doc('a[href^="https://www.v2ex.com/t/"]').items():
url = each.attr.href
if url.find('#reply')>:
url = url[:url.find('#')]
self.crawl(url, callback=self.detail_page, validate_cert=False)
for each in response.doc('a.page_normal').items():
self.crawl(each.attr.href, callback=self.board_page, validate_cert=False) @config(priority=)
def detail_page(self, response):
title = response.doc('h1').text()
content = response.doc('div.topic_content').html().replace('"', '\\"')
tmp = zip(response.doc('a[href^="/member/"]').items(), response.doc('div.reply_content').items())
reply_content = list()
for e1, e2 in tmp:
reply_content.append((e1.text(), e2.text()))
#self.add_question(title, content) #插入数据库
return {
"url": response.url,
"title": title,
"content": content,
"reply_content": reply_content,
}

示例代码二

#!/usr/bin/env python
# -*- encoding: utf- -*-
# Created on -- ::
# Project: tutorial_douban_movieimport re
from pyspider.libs.base_handler import *class Handler(BaseHandler):
"""
This is a sample script for: pyspider 爬虫教程(一):HTML 和 CSS 选择器
http://blog.binux.me/2015/01/pyspider-tutorial-level-1-html-and-css-selector/
""" @every(minutes= * )
def on_start(self):
self.crawl('http://movie.douban.com/tag/', callback=self.index_page) @config(age= * * )
def index_page(self, response):
for each in response.doc('a[href^="http"]').items():
if 'tag' in each.attr.href:
self.crawl(each.attr.href, callback=self.list_page) @config(age=***, priority=)
def list_page(self, response):
for each in response.doc('HTML>BODY>DIV#wrapper>DIV#content>DIV.grid-16-8.clearfix>DIV.article>DIV>TABLE TR.item>TD>DIV.pl2>A').items():
self.crawl(each.attr.href, priority=, callback=self.detail_page)
# 翻页
for each in response.doc('HTML>BODY>DIV#wrapper>DIV#content>DIV.grid-16-8.clearfix>DIV.article>DIV.paginator>A').items():
self.crawl(each.attr.href, callback=self.list_page) @config(priority=)
def detail_page(self, response):
return {
"url": response.url,
"title": response.doc('HTML>BODY>DIV#wrapper>DIV#content>H1>SPAN').text(),
"rating": response.doc('#interest_sectl > div.rating_wrap.clearbox > div.rating_self.clearfix > strong').text(),
"导演": [x.text() for x in response.doc('a[rel="v:directedBy"]').items()],
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,565
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905