使用Requests+xpath实现简单的数据爬取
来源:互联网 发布:淘宝热线电话 编辑:程序博客网 时间:2024/05/21 09:14
今天使用Requests+xpath实现简单的数据爬取,获取的是CSDN博客上的标题,发表时间,和阅读次数
下载PyCharm
这里我使用的是PyCharm
http://www.jetbrains.com/pycharm/download/download-thanks.html?platform=windows&code=PCC
关于PyCharm的使用方法,和AndroidStudio是一样的,这里不在多说。
正则表达式基础
- . 可以匹配任意字符,换行符\n除外
- 可以匹配前一个字符0次或无限次
- ?匹配前一个字符0次或者1次
- () 括号内的数据作为结果返回
常用方法
- findall: 匹配所有复合规律的内容,返回包含结果的列表
- Search: 匹配并提取第一个复合规律的内容,返回一个正则表达式对象
- Sub: 替换复合规律的内容,返回替换后的值
简单的正则demo
.的使用
a = "xzadkf"b = re.findall('x...',a)print b #输出['xzad']
*的使用
a = "xzaxkf"b = re.findall('x*',a)print b # ['x', '', '', 'x', '', '', '']
?的使用
a = "xzaxkf"b = re.findall('x?',a)print b # ['x', '', '', 'x', '', '', '']
rexStr = "adsfxxhelloxxiowengopfdwxxworldxxadjgoos"b = re.findall('xx.*xx',rexStr) # 满足条件尽可能多的查找print b # ['xxhelloxxiowengopfdwxxworldxx']c = re.findall('xx.*?xx',rexStr)print c # ['xxhelloxx', 'xxworldxx']d = re.findall('xx(.*?)xx',rexStr)print d # ['xxhelloxx', 'xxworldxx']hello = '''adsfxxhelloxxiowengopfdwxxworldxxadjgoos'''e = re.findall('xx(.*?)xx',hello,re.S)print e
获取标题
这里我们获取http://jp.tingroom.com/yuedu/yd300p/中的每个标题
html = requests.get('http://jp.tingroom.com/yuedu/yd300p/')html.encoding = 'utf-8'print html.texttitle = re.findall('color: #039;">(.*?)</a>',html.text,re.S)for each in title: print each
此时效果如下:
安装xpath
使用pip安装xpath
pip install xpath
抓取博客主页的内容
#.*-coding:utf-8-*-import requestsimport reimport sysreload(sys)sys.setdefaultencoding("utf-8")from lxml import etreeclass spider(object): # 获取url对应的网页源码 def getsource(self,url): headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0'} sourceHtml = requests.get(url, headers=headers) return sourceHtml.text # 改变链接的地址页 def chagnePage(self,orginalStr): currentPage = int(re.search('page=(\d+)', orginalStr, re.S).group(1)) pageGroup = [] for i in range(currentPage, currentPage + 3): link = re.sub('page=\d+', 'page=%s' % i, orginalStr, re.S) pageGroup.append(link) return pageGroup # 从html中解析我们需要的数据 def getNeedInfo(self,sourceHtml): currentAllInfo = [] selector = etree.HTML(sourceHtml) titles = selector.xpath('//dl[@class="blog_list clearfix"]//dd') singlePageInfo = {}; for vs in titles: info = vs.xpath('h3[@class="tracking-ad"]/a/text()') print "标题:" + info[0] singlePageInfo['title'] = info[0] time = vs.xpath('div[@class="blog_list_b clearfix"]/div[@class="blog_list_b_r fr"]/label/text()') print "时间:" + time[0] singlePageInfo['time'] = time[0] readCount = vs.xpath('div[@class="blog_list_b clearfix"]/div[@class="blog_list_b_r fr"]/span/em/text()') print "阅读次数:" + readCount[0] currentAllInfo.append(singlePageInfo) print currentAllInfoif __name__ == '__main__': spider = spider() url = "http://blog.csdn.net/?&page=1" allPage = spider.chagnePage(url) allPageInfo = [] for link in allPage: print '正在处理:'+link sourceHtml = spider.getsource(link) spider.getNeedInfo(sourceHtml)
0 0
- 使用Requests+xpath实现简单的数据爬取
- 基于PYTHON requests包,代理ip+header+延时爬取的简单实现
- requests和正则爬取猫眼的数据
- xpath的使用方法以及python爬取数据的保存
- python:使用requests,bs4爬取mmjpg上的图片
- 对Xpath爬取网页数据的理解
- 用requests爬取百度搜索数据
- Python requests爬虫爬取小说数据
- python +requests 实现爬取百度图片
- 使用requests爬取豆瓣《长城》影评
- 使用requests爬取豆瓣电影top250
- 实战 利用Xpath爬取网页数据
- requests爬取小说
- 使用Jsoup实现简单的页面信息爬取
- 利用Requests爬取图片与网页数据
- 利用bs4和requests爬取股票历史交易数据
- 用 Python requests库 爬取网页数据
- 用 requests 和 bs4 爬取世界大学排名数据
- Mysql学习笔记十七——表、视图的管理语句
- [Python笔记]1.python及Python库安装(windows)
- SOA
- CodeForces 701A Cards(水)
- IntelliJ IDEA 下的SVN使用(傻瓜式教学)
- 使用Requests+xpath实现简单的数据爬取
- 2017年阅读笔记整理
- 【数据结构基础】求二叉树高度
- Android 桌面插件首次安装不启动应用,直接拖出桌面插件不显示数据问题
- WebStorm破解
- LeetCode#22. Generate Parentheses
- 说反话 :给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出。
- Mysql学习笔记十八——存储引擎
- Java并发编程:volatile关键字解析