前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用流快速构建爬虫

使用流快速构建爬虫

作者头像
spark
发布2018-12-20 11:44:39
3860
发布2018-12-20 11:44:39
举报
文章被收录于专栏:数据科学

In [1]:

代码语言:javascript
复制
import streamz

from requests_html import HTMLSession
session = HTMLSession()

def get_response(url):
    global session
    return session.get(url)

def get_result(response):
    return response.html.search('<title>{}</title>'),response.url

def get_links(response):
    return response.html.absolute_links

def is_special_url(url):
    return 'gndy' in url

def is_special_response(response):
    return 'gndy' in response.url

source = streamz.Stream()
pages = source.unique()
response = pages.rate_limit(1).map(get_response)
special_response = response.filter(is_special_response)
result = special_response.map(get_result)
links = response.map(get_links).flatten()# list to stream
links.sink(source.emit)  # pipe new links back into pages

result.sink(print)

var element = $('#b35ad07b-1572-46ac-83be-ba4a3d8906b4'); {"model_id": "85e89a40c56d478b99eb2f93bdeb249a", "version_major": 2, "version_minor": 0}

In [2]:

代码语言:javascript
复制
source.visualize()

Out[2]:

In [3]:

代码语言:javascript
复制
source.emit('http://www.dytt8.net')
代码语言:javascript
复制
(<Result ('2017年喜剧《疯狂成名记》BD中英双字幕迅雷下载_电影天堂',) {}>, 'https://www.dytt8.net/html/gndy/jddy/20181026/57678.html')
(<Result ('2008爱情《李米的猜想》HD国语中文字幕迅雷下载_电影天堂',) {}>, 'https://www.dytt8.net/html/gndy/jddy/20170418/53762.html')
(<Result ('2018年奇幻剧情《画皮师2》HD国语中字迅雷下载_电影天堂',) {}>, 'https://www.dytt8.net/html/gndy/jddy/20181021/57657.html')
(<Result ('2018年喜剧《公主大对换》BD中英双字幕迅雷下载_电影天堂',) {}>, 'https://www.dytt8.net/html/gndy/dyzz/20181117/57804.html')
代码语言:javascript
复制
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-3-16a897b490c4> in <module>()
----> 1 source.emit('http://www.dytt8.net')

/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/streamz/core.py in emit(self, x, asynchronous)
    330 
    331                 raise gen.Return(result)
--> 332             sync(self.loop, _)
    333 
    334     def update(self, x, who=None):

/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/streamz/core.py in sync(loop, func, *args, **kwargs)
   1273     else:
   1274         while not e.is_set():
-> 1275             e.wait(10)
   1276     if error[0]:
   1277         six.reraise(*error[0])

/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py in wait(self, timeout)
    550             signaled = self._flag
    551             if not signaled:
--> 552                 signaled = self._cond.wait(timeout)
    553             return signaled
    554 

/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py in wait(self, timeout)
    298             else:
    299                 if timeout > 0:
--> 300                     gotit = waiter.acquire(True, timeout)
    301                 else:
    302                     gotit = waiter.acquire(False)

KeyboardInterrupt: 
代码语言:javascript
复制
(<Result ('2018年剧情《三角草的春天》BD日语中字迅雷下载_电影天堂',) {}>, 'https://www.dytt8.net/html/gndy/dyzz/20181031/57698.html')
(<Result ('2018年动作《蜀山降魔传》BD国粤双语中字迅雷下载_电影天堂',) {}>, 'https://www.dytt8.net/html/gndy/dyzz/20181015/57623.html')
(<Result ('2018年科幻动作《铁血战士》HD韩版中字迅雷下载_电影天堂',) {}>, 'https://www.dytt8.net/html/gndy/dyzz/20181114/57791.html')
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018年11月19日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档