python爬虫2
来源:互联网 发布:java 超级打码 编辑:程序博客网 时间:2024/06/06 03:21
**正则表达式的应用举例
使用findall与search从大量文本中匹配感兴趣的内容
使用sub实现换页功能**
正则表达式的应用举例 — 匹配多段内容
灵活使用findall与search
先抓大再抓小
正则表达式的应用举例 — 实现翻页
实验网址:http://www.jikexueyuan.com/course/android/?pageNum=2
核心代码
new_link = re.sub(‘pageNum=\d+’,’pageNum=%d’%i, old_url)
其中text.txt文件内容
<html> <head> <title>极客学院爬虫测试</title> </head> <body> <div class="topic"><a href="http://jikexueyuan.com/welcome.html">欢迎参加《Python定向爬虫入门课程》</a> <div class="list"> <ul> <li><a href="http://jikexueyuan.com/1.html">这是第一条</a></li> <li><a href="http://jikexueyuan.com/2.html">这是第二条</a></li> <li><a href="http://jikexueyuan.com/3.html">这是第三条</a></li> </ul> </div> </div> </body></html>
#-*-coding:utf8-*-#导入re库文件import reold_url = 'http://www.jikexueyuan.com/course/android/?pageNum=2'total_page = 20f = open('text.txt','r')html = f.read()f.close()#爬取标题# title = re.search('<title>(.*?)</title>',html,re.S).group(1)# print title#爬取链接# links = re.findall('href="(.*?)"',html,re.S)# for each in links:# print each#抓取部分文字,先大再小# text_fied = re.findall('<ul>(.*?)</ul>',html,re.S)[0]# the_text = re.findall('">(.*?)</a>',text_fied,re.S)# for every_text in the_text:# print every_text#sub实现翻页for i in range(2,total_page+1): new_link = re.sub('pageNum=\d+','pageNum=%d'%i,old_url,re.S) print new_link
0 0
- python爬虫初学(2)
- python网络爬虫2
- python爬虫2
- [Python]网络爬虫2
- Python 爬虫学习2
- Python爬虫基础-2
- python爬虫练习2
- python 爬虫随笔2
- Python爬虫----爬虫入门(2)
- python爬虫(2-2)
- python 3.5 爬虫日记2
- Python爬虫(bs4)-2
- Python爬虫(2),Python3.x
- python 3.0 网络爬虫 2
- python 爬虫 教程(2)
- Python 2 的网络爬虫
- 用python 2运行爬虫
- python爬虫入门到懵逼-2
- HBase1.0.1基本操作(java代码)
- hdu 2523SORT AGAIN
- 自定义cell及其使用
- UITableView 的编辑
- 深挖BAT内部级别和薪资待遇,你敢看?
- python爬虫2
- 简单多图片上传 jquery+java 代码
- hdu5371 Hotaru's problem manachar回文串算法+枚举
- 黑马程序员——java复习总结——集合框架
- OC 将汉语数字转换成阿拉伯数字
- HDU 5387 Clock
- KMP算法详解
- uboot移植i2c
- 二叉查找树的java实现