美文网首页
Pyspider 示例代码运行的相关问题

Pyspider 示例代码运行的相关问题

作者: 万事皆成 | 来源:发表于2018-10-25 22:14 被阅读8次

Level 1: HTML and CSS Selector

官网提供代码运行结果不对,修改如下:

import re
from pyspider.libs.base_handler import *


class Handler(BaseHandler):
    crawl_config = {
    }

    @every(minutes=24 * 60)
    def on_start(self):
        self.crawl('http://www.imdb.com/search/title?count=100&title_type=feature,tv_series,tv_movie&ref_=nv_ch_mm_1', callback=self.index_page)


    @config(age=10 * 24 * 60 * 60)
    def index_page(self, response):
        for each in response.doc('a[href^="https"]').items():
            if re.match("https://www.imdb.com/title/tt\d+/\?ref_=", each.attr.href):
                movie_group = re.match('(https://www.imdb.com/title/tt\d+/).*', each.attr.href)
                self.crawl(movie_group.groups()[0], callback=self.detail_page)
                
    @config(priority=2)
    def detail_page(self, response):
        
        title = re.search('<h1 class="">(.*?)<', response.text).group(1).replace('&nbsp;', '').strip()
        
        item_list = response.doc('.credit_summary_item').items()
        director = []
        for item in item_list:
            if "Director" in item('h4').text():
                director = [x.text() for x in item('a').items()]
                          
        return {
                "url": response.url,
                "title": title,
                "rating": response.doc('[itemprop="ratingValue"]').text(),
                "director": director,
               }

Level 2: AJAX and More HTTP

使用 Postman 模拟请求失败 待完成!!!
通过浏览器跟踪不到原有 XHR 数据接口: http://api.twitch.tv/kraken/streams?limit=20&offset=0&game=Dota+2&broadcaster_language=&on_site=1
新的 json 数据请求为: https://gql.twitch.tv/gql
header:

POST /gql HTTP/1.1
Host: gql.twitch.tv
Connection: keep-alive
Content-Length: 255
Pragma: no-cache
Cache-Control: no-cache
Origin: https://www.twitch.tv
Accept-Language: zh-CN
Client-Id: kimne78kx3ncx6brgo4mv6wki5h1ko
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36
X-Device-Id: 326baa0403887e01
Content-Type: text/plain;charset=UTF-8
Accept: */*
Referer: https://www.twitch.tv/directory/game/Dota%202
Accept-Encoding: gzip, deflate, br

payload:

[{"operationName":"DirectoryPage_Game","variables":{"name":"dota 2","limit":30,"sort":"VIEWER_COUNT","tags":[],"cursor":"Nzc="},"extensions":{"persistedQuery":{"version":1,"sha256Hash":"f7c5ea69517715f8ab06d30ce66f6355af61593ac0ff806b518286932d177cc7"}}}]

Level 3: Render with PhantomJS

使用 PhantomJS 获取页面 http://www.twitch.tv/directory/game/Dota%202 失败!!!

成功: pyspider 爬虫教程(三):使用 PhantomJS 渲染带 JS 的页面

问题: 浏览器中能获取到的 dom,pyspider + phantomjs 获取不到
解决方法:在项目列表中,将项目的状态设置为 debug 或者 running,再重新运行项目

相关文章

  • Pyspider 示例代码运行的相关问题

    Level 1: HTML and CSS Selector 官网提供代码运行结果不对,修改如下: Level 2...

  • day12-json文件和异常处理

    1.文件操作 代码示例 代码示例 2.json文件(重要) 代码示例 运行结果 代码示例 代码示例 运行结果 代码...

  • 10.15 day12 json文件和异常处理

    1.文件操作 代码示例 代码示例 2.json文件(重要) 代码示例 运行结果 代码示例 代码示例 运行结果 代码...

  • 业务C代码关键审核点总结

    工作有几年了,定位过许多起业务软件运行异常问题,相关问题代码易错点整理如下,示例代码语法上不严谨,供理解参考:(1...

  • day16-面向对象和pygame

    1.类的继承 代码示例 运行结果 2.重写 代码示例 运行结果 3.添加属性 代码示例 运行结果 练习 代码如下 ...

  • day18-正则表达式

    1.正则表达式语法 代码示例 运行结果 2.正则表达式次数相关符号 代码示例 运行结果 3.分支和分组 1.|分支...

  • 10.23 - 正则表达式

    1.正则表达式语法 代码示例 运行结果 2.正则表达式次数相关符号 代码示例 运行结果 3.分支和分组 1.|分支...

  • day11-文件得操作-总结

    1.模块得使用 代码示例 运行如下 2.选择性导入(阻止导入) 3.迭代器 代码示例 运行结果 代码示例 运行结果...

  • 10.12 day11 文件得操作

    1.模块得使用 代码示例 运行如下 2.选择性导入(阻止导入) 3.迭代器 代码示例 运行结果 代码示例 运行结果...

  • js下滑加载更多

    示例代码:查看代码 查看运行

网友评论

      本文标题:Pyspider 示例代码运行的相关问题

      本文链接:https://www.haomeiwen.com/subject/rlhhtqtx.html