马拉松赛事日历

马拉松赛事日历(2016)

通过python+scrapy获取在中国田协注册的马拉松赛事日历(2016) www.runchina.org.cn

环境配置

Windows
Python 2.7
PyMongo
Scrapy

爬取的规则

class Spider(CrawlSpider):
name = "marathon"
start_urls = [
"http://www.runchina.org.cn/portal.php?mod=calendar&ac=list&year=2016&month=01&project=0"
]
i = 2;
def parse(self,response):
if(self.i>13):
return
selector = Selector(response)
ul = selector.xpath('//ul[@class="match-list"]/li')
matchs = ul.xpath('div[@class="match-item"]')
#logger.info('====================== %s,%s ',len(ul),len(matchs))
for match in matchs:
marathon = RunchinaItem()
marathon['_id'] = str(ObjectId())
marathon['name'] = match.xpath('span[@class="match-name text-omit"]/text()').extract_first()
marathon['addr'] = match.xpath('span[@class="match-info text-omit"]/text()').extract_first()
marathon['time'] = match.xpath('span[@class="match-info text-omit"][2]/text()').extract_first()
marathon['website'] = time = match.xpath('span[@class="match-info text-omit"]/a/@href').extract_first()
#logger.info('========= addr ====== %s ',link)
yield marathon
if(self.i >= 10):
month = str(self.i)
else:
month = '0'+str(self.i)
next_month = "http://www.runchina.org.cn/portal.php?mod=calendar&ac=list&year=2016&month=%s&project=0" % month
if(next_month):
self.i += 1
yield Request(url=next_month,callback=self.parse)

代码

源码

下载之后,直接运行 python run.py

运行示意图
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,083评论 19 139
  • scrapy提供了两种提取方法,Xpath和Css。 Xpath用起来觉得好麻烦,有点笨,我还是比较喜欢Css的提...
    阿轲666阅读 3,557评论 0 4
  • 我开始逐渐接受你晦涩难懂的背面,以及表象之后,我们所拥有的那一些不那么体面的东西。 比如,坏脾气、睡眠障碍和无端端...
    土豆公主阅读 325评论 0 2
  • 我不会的东西一定是重要的东西,一定要学。你老是告诉自己这不重要,这我不会,无所谓,这你就老了。~~~~陈虻
    心理咨询师萍阅读 334评论 2 0
  • 你的目光轻轻掠过我的脸庞, 我就无处躲藏。 不知道你是否能看见, 我眼中的火光, 闪闪发亮。 ...
    一斗米呀阅读 228评论 0 6