Coder Social home page Coder Social logo

Comments (8)

michael-liumh avatar michael-liumh commented on September 28, 2024

原来可以用process_failed_response重新定义

from ruia.

howie6879 avatar howie6879 commented on September 28, 2024

对的,问题解决了么

from ruia.

michael-liumh avatar michael-liumh commented on September 28, 2024

没呢,我现在在爬取一个代理网站的免费代理,现在的需求是将代理ip爬取下来后,对这些代理进行检测,看是否有效,检测的方式很简单,就是访问 http://httpbin.org/ip 这个地址,看有没由response返回,但暂时无法实现。

原因是爬取完代理ip后,我不知道如何重新组装一个新的request去检测。

我尝试定义2个spider,现有spider 1将代理网站的代理ip爬取下来后,使用spider 2来对这些ip进行检测,但一个py文件无法直接运行2个spider,会报错,说aiogttp的会话已经在运行了

from ruia.

michael-liumh avatar michael-liumh commented on September 28, 2024

具体代码如下:

# !/usr/bin/env python3
# -*- coding:utf8 -*-

from ruia import Item, Spider, TextField, Request
from ruia_ua import middleware
import sys
# import aiofiles
# import asyncio
# import aiohttp

PROXIES_LIST = []
PROXIES_FILE = 'proxies.json'
CHECK_URL = "http://httpbin.org/ip"


class KuaiDailiItem(Item):
    """
    定义爬虫的目标字段
    """
    target_item = TextField(xpath_select='//table[@class="table table-bordered table-striped"]')
    ip = TextField(xpath_select='.//td[@data-title="IP"]', many=True, default='could not get ips')
    port = TextField(xpath_select='.//td[@data-title="PORT"]', many=True, default='could not get ports')
    schema = TextField(xpath_select='.//td[@data-title="类型"]', many=True, default='could not get schemas')


class KuaiDailiSpider(Spider):
    start_urls = [f'https://www.kuaidaili.com/free/inha/{index}/' for index in range(1, 2)]
    aiohttp_kwargs = {
        "allow_redirects": True
    }

    async def parse(self, response):
        if response.status == 200:
            # if response.url == CHECK_URL:
            #     print(response.text())
            # else:
            async for item in KuaiDailiItem.get_items(html=await response.text()):
                yield item
        else:
            self.logger.error("爬取目标网页 {} 源代理失败,状态码为:{}".format(response.url, response.status))

    async def process_item(self, item: KuaiDailiItem):
        for ip, port, schema in zip(item.ip, item.port, item.schema):
            proxy = schema.lower() + "://" + ip + ":" + port
            await self.check_proxy(proxy)

    async def check_proxy(self, proxy: str = ''):
        if proxy:
            print("checking proxy:", proxy)
            self.aiohttp_kwargs['proxy'] = proxy
            req = self.request(url=CHECK_URL, **self.aiohttp_kwargs)
            await self.handle_request(req)
            # print(await response.text())


def main(*args):
    KuaiDailiSpider.start(middleware=middleware)


if __name__ == "__main__":
    main(*sys.argv[1:])

目前无法实现异步检测

from ruia.

howie6879 avatar howie6879 commented on September 28, 2024

这种网站的东西不是一次检测有效就永远有效,你应该写一个调度来定时检测是否有效

from ruia.

howie6879 avatar howie6879 commented on September 28, 2024

我个人项目下面应该有个代理池相关项目,你可以看看有没有用

from ruia.

michael-liumh avatar michael-liumh commented on September 28, 2024

好的,感谢

from ruia.

howie6879 avatar howie6879 commented on September 28, 2024

问题解决了么

from ruia.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.