github 地址: https://github.com/gaojiuli/gain/
gain 是为了让每大家能够轻松编写 python 爬虫, 它使用了 asyncio, uvloop 和 aiohttp.
pip install gain
Write spider.py:
from gain import Css, Item, Parser, Spider class Post(Item): title = Css('.entry-title') cOntent= Css('.entry-content') async def save(self): with open('scrapinghub.txt', 'a+') as f: f.writelines(self.results['title'] + '\n') class MySpider(Spider): start_url = 'https://blog.scrapinghub.com/' parsers = [Parser('https://blog.scrapinghub.com/page/\d+/'), Parser('https://blog.scrapinghub.com/\d{4}/\d{2}/\d{2}/[a-z0-9\-]+/', Post)] MySpider.run()
run python spider.py
案例在 /example/
目录下.
github 地址: https://github.com/gaojiuli/gain/
![]() | 1 awolfly9 2017-06-02 17:35:46 +08:00 via Android mark |
2 MIROKY 2017-06-02 17:49:27 +08:00 via Android wow 马克 |
3 charove 2017-06-02 17:50:27 +08:00 感觉好叼。。。 |
![]() | 4 qs 2017-06-02 20:13:08 +08:00 win 平台不支持 uvloop, 建议做个适配 |
6 maze1024 2017-06-03 00:30:49 +08:00 aiohttp 的 http 解析配合 uvloop 不是很高效,建议看看 uvloop 的实现 https://github.com/MagicStack/httptools |
![]() | 7 isaced 2017-06-03 00:52:48 +08:00 建议把输出结果文件的操作封装一下用起来更舒服 |
8 PythonAnswer 2017-06-03 01:07:49 +08:00 via Android uvloop win 跑不了啊 |
![]() | 10 prasanta OP @isaced 你有什么好的想法么,我现在是让使用者自定义 save()函数。不知道你的封装是指? |
![]() | 11 prasanta OP @PythonAnswer 看了楼上的意见,我准备暂时移除 uvloop |
![]() | 12 chuanqirenwu 2017-06-03 10:14:59 +08:00 不错,感谢作者的分享,已转发到 Pythonzhcn 社区,不知道是否允许? |
![]() | 13 prasanta OP @chuanqirenwu 可以 |
![]() | 14 pythonee 2017-06-03 20:17:46 +08:00 mark |
![]() | 15 hellogbk 2017-06-04 00:51:49 +08:00 我这些天在用 pyquery 的时候是到一个问题,如果网页是以 <?xml version="1.0" encoding="UTF-8"?> 开头的则 pyquery 会出错。 不知道楼主有没有遇到。 |
17 hbmask 2017-06-04 10:50:47 +08:00 mark 一下 |
![]() | 18 hellogbk 2017-06-04 13:27:37 +08:00 |
![]() | 20 pb941129 2017-06-04 22:42:58 +08:00 mark 一下 |