我的简单爬虫
simple-crawler的Python项目详细描述
安装
pip3 install simple-crawler
将environmentAUTO_CHARSET=1
设置为passbytes
以美化oup4并让它检测字符集。
课程
URL
:定义一个urlURLExt
:要处理的类URL
Page
:定义URL
的请求结果url
:类型URL
content
,text
,json
:来自库的响应内容属性requests
type
:响应体类型是一个枚举,它允许BYTES
、TEXT
、HTML
、JSON
is_html
:检查html是否符合响应头的Content-Type
soup
:BeautifulSoup
如果is_html
Crawler
:通过反复调用handler_page()
来调度爬网程序
示例
from simple_crawler import *
class MyCrawler(Crawler):
name = 'output.txt'
aysnc def custom_handle_page(self, page):
print(page.url)
tags = page.soup.select("#container")
tag = tags and tags[0]
with open(self.name, 'a') as f:
f.write(tag.text)
# do some async call
def filter_url(self, url: URL) -> bool:
return url.url.startswith("https://xxx.com/xxx")
loop = get_event_loop(True)
c = MyCrawler("https://xxx.com/xxx", loop, concurrency=10)
schedule_future_in_loop(c.start(), loop=loop)
待办事项
- [X]使用异步或线程加速