简单的python爬取网页字符串内容并保存
2016-05-17 23:07
471 查看
最近想试试python的爬虫库,就找了个只有字符串的的网页来爬取。网址如下:
http://mobilecdn.kugou.com/api/v3/special/song?plat=0&page=1&pagesize=-1&version=7993&with_res_tag=1&specialid=26430
打开后看到是一些歌名还有hash等信息。按照hash|filename的方式存在文件里,先贴代码
说起来也比较简单,就是拿到取html页面后按照正则取两次内容后存在txt里面。
http://mobilecdn.kugou.com/api/v3/special/song?plat=0&page=1&pagesize=-1&version=7993&with_res_tag=1&specialid=26430
打开后看到是一些歌名还有hash等信息。按照hash|filename的方式存在文件里,先贴代码
#coding=utf-8 import urllib import re import os def getHtml(url): page = urllib.urlopen(url) html = page.read() return html def getHash(html): reg = r'"hash":"(.+?)",' has = re.compile(reg) hashlist = re.findall(has,html) with open('1.txt','w') as f: for has in hashlist: f.write(has+"|"+"\r\n") def getName(html): reg=r'"filename":"(.+?)",' name=re.compile(reg) namelist=re.findall(name,html) with open('1.txt','rb') as fr: with open('2.txt','wb') as fw: for name in namelist: for l in fr: fw.write(l.replace(b'\r\n', name+b'\r\n')) break html=getHtml("http://mobilecdn.kugou.com/api/v3/special/song?plat=0&page=1&pagesize=-1&version=7993&with_res_tag=1&specialid=26430") getHash(html) getName(html) os.remove('1.txt')
说起来也比较简单,就是拿到取html页面后按照正则取两次内容后存在txt里面。
相关文章推荐
- python一些内建函数(map,zip,filter,reduce,yield等)
- Python之常用模块(待更新)
- Tornado X MVC
- [Python笔记]第九篇:re正则表达式
- python
- python学习笔记5—数据类型转换
- python range() 和xrange()的区别
- python交互模式下方向键、退格键出现乱码及ipython的安装
- 练习010
- 理解Python中的with…as…语法
- Python scrapy 实现网页爬虫
- 用字典创建一个平台的用户信息(包含用户名和密码)管理系统
- LeetCode Reverse Words in a String
- python笔记之ArcPy简介
- 用交叉验证改善模型的预测表现(适用于Python和R)
- python笔记之ArcPy函数列表
- Python Day2 数据类型: 列表元组和字典
- 关于Python验证码识别安装PIL、tesseract-ocr与pytesseract模块的错误解决
- 图的DFS遍历(python版本)
- Python is和==区别