Python 爬虫学习:爬取LeetCode的题目并且按照难度分类
来源:互联网 发布:淘宝usa商城 假货 编辑:程序博客网 时间:2024/05/16 06:40
过程:
爬取LeeCode的所有题目,按照难度的分类。在爬取题号的时候比较麻烦,要判断一个字符串是不是整数,首先想到的是用
Code:
# -*- coding: utf-8 -*-__author__ = 'bigship'import urllib2import stringdef func(x):#判断一个字符串是不是整数 try: x=int(x) return x except ValueError: return Falseurl = 'https://leetcode.com/problemset/algorithms/'Easy = {}Medium = {}Hard={}ALL={}head = '''<a href="/problems/'''tail = "</a>"headnum='<td>'tailnum="</td>"response = urllib2.urlopen(url)html = response.read()numA=0numE=0numM=0numH=0numhead=html.find(headnum)while numhead!=-1: numtail = html.find(tailnum,numhead+1) if func(html[numhead+len(headnum):numtail])!=False: numA+=1 poshead = html.find(head,numtail+1) postail =html.find(tail,poshead+1) poshead = html.find('''/">''',poshead+1) pos1 = html.find("""<td value='""",postail+1) pos1 = html.find(">",pos1+1) pos2 = html.find("</td>",pos1+1) print html[numhead+len(headnum):numtail]+" "+html[poshead+3:postail]+" "+html[pos1+1:pos2] ALL[func(html[numhead+len(headnum):numtail])]=html[poshead+3:postail] if html[pos1+1:pos2]=='Easy': Easy[func(html[numhead+len(headnum):numtail])]=html[poshead+3:postail] numE+=1 if html[pos1+1:pos2]=='Medium': Medium[func(html[numhead+len(headnum):numtail])]=html[poshead+3:postail] numM+=1 if html[pos1+1:pos2]=='Hard': Hard[func(html[numhead+len(headnum):numtail])]=html[poshead+3:postail] numH+=1 numhead=html.find(headnum,numhead+1)print "总题数 :"+str(numA)print "简单题数 :"+str(numE)print "中等题数 :"+str(numM)print "难题数 :"+str(numH)
1 0
- Python 爬虫学习:爬取LeetCode的题目并且按照难度分类
- Python爬虫学习笔记(1):简单的图片爬取
- Python网络爬虫——爬取POJ题目
- python爬虫抓取LeetCode题目
- leetcode题目的难度和面试频率
- LeetCode 分类难度总结
- python爬虫学习--pixiv爬虫(2)--国际排行榜的图片爬取
- python网络爬虫学习(二)一个爬取百度贴吧的爬虫程序
- leetcode 题目总结(暂时按照此分类刷)
- python爬虫爬取网上的照片
- python爬虫----简单的图片爬取
- python爬虫爬取好看的图片
- 【Python网络爬虫学习01】爬取网络验证码
- Python爬虫学习笔记-网页topN条目爬取
- Python 爬虫学习2爬取租房网站信息
- python爬虫学习(股票数据爬取)
- PYTHON爬虫学习-糗事百科内容爬取
- python爬虫学习笔记(1)-爬取糗事百科
- 基数排序算法之Java实现
- iOS 简单弹幕效果
- iOS开发小技巧:刷新UITableView
- 电脑不插电源启动就没声音的问题
- Spark 数据挖掘 - 利用决策树预测森林覆盖类型
- Python 爬虫学习:爬取LeetCode的题目并且按照难度分类
- A PAPER LIST FOR OBJECT DETECTION IN VIDEOS
- 线程的分类
- hiho 挑战赛16 B 王胖浩与环
- 阿里云ubuntu14.04 64位 java web环境的搭建
- ajax对象使用的简单实例
- 仿函数
- 在中国爱国企业家联盟我们应微笑应对生活
- ViewPager