Comments (8)
原来可以用process_failed_response重新定义
from ruia.
对的,问题解决了么
from ruia.
没呢,我现在在爬取一个代理网站的免费代理,现在的需求是将代理ip爬取下来后,对这些代理进行检测,看是否有效,检测的方式很简单,就是访问 http://httpbin.org/ip 这个地址,看有没由response返回,但暂时无法实现。
原因是爬取完代理ip后,我不知道如何重新组装一个新的request去检测。
我尝试定义2个spider,现有spider 1将代理网站的代理ip爬取下来后,使用spider 2来对这些ip进行检测,但一个py文件无法直接运行2个spider,会报错,说aiogttp的会话已经在运行了
from ruia.
具体代码如下:
# !/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.
这种网站的东西不是一次检测有效就永远有效,你应该写一个调度来定时检测是否有效
from ruia.
我个人项目下面应该有个代理池相关项目,你可以看看有没有用
from ruia.
好的,感谢
from ruia.
问题解决了么
from ruia.
Related Issues (20)
- Is it possible to use SOCKS5 proxy? HOT 9
- Improve Chinese documentation
- Would be nice to be able to pass in "start_urls" HOT 7
- Trouble scraping deck.tk/deckstats.net HOT 7
- python3.9 remove asyncio.Task.all_tasks() HOT 3
- 运行示例代码报错 HOT 10
- 代理使用问题 HOT 1
- 是否可以用模式匹配工具-pampy来实现对json解析的支持 HOT 1
- 并发5,循环爬取1000个网页,CPU耗尽为0,但是内存没有耗完,大佬帮看看代码有什么问题 HOT 3
- POST发送请求,收不到请求中的body HOT 2
- httpx替换aiohttp支持http2 HOT 1
- 我应当如何向 Spider 传递 start_urls? HOT 1
- 示例代码运行报错 HOT 3
- worker_numbers 数值多少合适 HOT 1
- ruia 使用lxml编码xml文档时报错 HOT 1
- 希望添加更多功能,更多示例,更多文档,希望长期维护~
- 通过中间件添加 socks5 代理后如何关闭 session?
- docs.python-ruia.org is not available HOT 4
- 如果能支持分布式就好了
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ruia.