Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #
  2. # Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. from abc import ABC
  17. import asyncio
  18. from crawl4ai import AsyncWebCrawler
  19. from agent.component.base import ComponentBase, ComponentParamBase
  20. class CrawlerParam(ComponentParamBase):
  21. """
  22. Define the Crawler component parameters.
  23. """
  24. def __init__(self):
  25. super().__init__()
  26. self.proxy = None
  27. self.extract_type = "markdown"
  28. def check(self):
  29. self.check_valid_value(self.extract_type, "Type of content from the crawler", ['html', 'markdown', 'content'])
  30. class Crawler(ComponentBase, ABC):
  31. component_name = "Crawler"
  32. def _run(self, history, **kwargs):
  33. ans = self.get_input()
  34. ans = " - ".join(ans["content"]) if "content" in ans else ""
  35. if not ans:
  36. return Crawler.be_output("")
  37. try:
  38. result = asyncio.run(self.get_web(ans))
  39. return Crawler.be_output(result)
  40. except Exception as e:
  41. return Crawler.be_output(f"An unexpected error occurred: {str(e)}")
  42. async def get_web(self, url):
  43. proxy = self._param.proxy if self._param.proxy else None
  44. async with AsyncWebCrawler(verbose=True, proxy=proxy) as crawler:
  45. result = await crawler.arun(
  46. url=url,
  47. bypass_cache=True
  48. )
  49. if self._param.extract_type == 'html':
  50. return result.cleaned_html
  51. elif self._param.extract_type == 'markdown':
  52. return result.markdown
  53. elif self._param.extract_type == 'content':
  54. result.extracted_content
  55. return result.markdown