python爬虫入门(2)re模块-正则表达式
正则表达式
import re
re.search(r'(([01]{0,1}\d{0,1}\d|2[0-4]\d|25[0-5])\.){3}([01]{0,1}\d{0,1}\d|2[0-4]\d|25[0-5])','192.168.1.1')
>>> p = re.compile('\d+')
>>> p.findall('3只小甲鱼,15条腿,多出的3条在哪里?')
['3','15','3']
>>>import re
>>> p = re.compile('[a-z]+')
>>> p
re.compile('[a-z]+')
>>> p.match("")
>>>print(p.match(""))
None
>>> m = p.match('fishc')
>>> m
<_sre.SRE_Match object; span=(0,5), match='fishc'>
>>> m.group()
'fishc'
>>> m.start()
0
>>> m.end()
5
>>> m.span()
(0,5)
- 设置了编译标志符
charref = re.compile(r"""
&[#] # 开始数字引用
(
0[0-7]+ # 八进制格式
| [0-9]+ # 十进制格式
| x[0-9a-fA-F]+ # 十六进制格式
)
; # 结尾分号
""", re.VERBOSE)
- 未设置编译标志符
charref = re.compile("&#(0[0-7]+|[0-9]+|x[0-9a-fA-F]+);")
import urllib.request
import re
def open_url(url):
req = urllib.request.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36')
page = urllib.request.urlopen(req)#获取页面内容
html = page.read().decode('utf-8')#解码页面内容
return html def get_img(html):
p = r'(?:(?:[0,1]?\d?\d|2[0-4]\d|25[0-5])\.){3}(?:[0,1]?\d?\d|2[0-4]\d|25[0-5])' #匹配IP地址
iplist = re.findall(p,html)
for each in iplist:
print(each) if __name__ =='__main__':
url ="http://www.xicidaili.com/"
get_img(open_url(url))
import urllib.request
import os
import re def save_imgs(folder, img_addrs):
for each in img_addrs:
filename = each.split('/')[-1]
with open(filename, 'wb') as f:
img = url_open(each)
f.write(img)
print(1)
print(2) def url_open(url):
reg = urllib.request.Request(url)
reg.add_header('User-Agent',
'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36')
response = urllib.request.urlopen(url)
html = response.read() return html def get_page(url):
html = url_open(url).decode('utf-8') # a = html.find('current-comment-page')+23
a = re.search(r'\[\d{1,4}\]', html)
a = a.group()
b = len(a)
a = a[1:b - 1]
return a def find_imgs(url):
html = url_open(url).decode('utf-8')
img_addrs = []
a = html.find('img src=')
while a != -1:
b = html.find('.jpg', a, a + 255)
if b != -1:
if 'lanya' in html[a + 9:b + 4]:
pass
else:
img_addrs.append('http:' + html[a + 9:b + 4])
else:
b = a + 9
a = html.find('img src=', b)
print(img_addrs)
return img_addrs def download_mm(folder='ooxx', pages=4, star=0):
os.mkdir(folder)
os.chdir(folder) url = 'http://jandan.net/ooxx/' # 妹子图地址
# url = 'http://jandan.net/pic/' #无聊图地址
if star != 0:
page_num = star
else:
page_num = int(get_page(url))
for i in range(pages):
page_num -= 1
page_url = url + 'page-' + str(page_num) + '#comments' img_addrs = find_imgs(page_url) save_imgs(folder, img_addrs) print(page_url) if __name__ == '__main__':
download_mm()
python爬虫入门(2)re模块-正则表达式的更多相关文章
- Python爬虫入门之正则表达式
在前面我们已经搞定了怎样获取页面的内容,不过还差一步,这么多杂乱的代码夹杂文字我们怎样把它提取出来整理呢?下面就开始介绍一个十分强大的工具,正则表达式! 1.了解正则表达式 正则表达式是对字符串操作的 ...
- Python爬虫入门七之正则表达式
在前面我们已经搞定了怎样获取页面的内容,不过还差一步,这么多杂乱的代码夹杂文字我们怎样把它提取出来整理呢?下面就开始介绍一个十分强大的工具,正则表达式! 1.了解正则表达式 正则表达式是对字符串操作的 ...
- 转 Python爬虫入门七之正则表达式
静觅 » Python爬虫入门七之正则表达式 1.了解正则表达式 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串 ...
- Python爬虫与数据分析之模块:内置模块、开源模块、自定义模块
专栏目录: Python爬虫与数据分析之python教学视频.python源码分享,python Python爬虫与数据分析之基础教程:Python的语法.字典.元组.列表 Python爬虫与数据分析 ...
- Python爬虫入门六之Cookie的使用
大家好哈,上一节我们研究了一下爬虫的异常处理问题,那么接下来我们一起来看一下Cookie的使用. 为什么要使用Cookie呢? Cookie,指某些网站为了辨别用户身份.进行session跟踪而储存在 ...
- Python爬虫入门一之综述
大家好哈,最近博主在学习Python,学习期间也遇到一些问题,获得了一些经验,在此将自己的学习系统地整理下来,如果大家有兴趣学习爬虫的话,可以将这些文章作为参考,也欢迎大家一共分享学习经验. Pyth ...
- Python爬虫入门教程 48-100 使用mitmdump抓取手机惠农APP-手机APP爬虫部分
1. 爬取前的分析 mitmdump是mitmproxy的命令行接口,比Fiddler.Charles等工具方便的地方是它可以对接Python脚本. 有了它我们可以不用手动截获和分析HTTP请求和响应 ...
- Python爬虫入门教程 43-100 百思不得姐APP数据-手机APP爬虫部分
1. Python爬虫入门教程 爬取背景 2019年1月10日深夜,打开了百思不得姐APP,想了一下是否可以爬呢?不自觉的安装到了夜神模拟器里面.这个APP还是比较有名和有意思的. 下面是百思不得姐的 ...
- Python爬虫入门之Cookie的使用
本节我们一起来看一下Cookie的使用. 为什么要使用Cookie呢? Cookie,指某些网站为了辨别用户身份.进行session跟踪而储存在用户本地终端上的数据(通常经过加密) 比如说有些网站需要 ...
- 1.Python爬虫入门一之综述
要学习Python爬虫,我们要学习的共有以下几点: Python基础知识 Python中urllib和urllib2库的用法 Python正则表达式 Python爬虫框架Scrapy Python爬虫 ...
随机推荐
- Ubuntu16.04多个版本GCC编译器的安装和切换【转】
本文转载自:https://www.cnblogs.com/uestc-mm/p/7511063.html 这几天在配置交叉编译ARM开发板的linux内核的过程中碰到了很多问题,其中包括了GCC版本 ...
- shell小脚本--从laod博客更新hosts文件
#!/bin/bash #-------------------------------------------- # name: change-hosts.sh #----------------- ...
- ubuntu 18.04 64bit下如何安装python开发工具jupyter
1.执行一下命令进行安装 sudo apt-get install python3-distutils wget https://bootstrap.pypa.io/get-pip.py sudo p ...
- Commons Configuration之二基本特性和AbstractConfiguration
Configuration接口定义一大堆方法.一切从头开始实现这些方法非常困难.因为AbstractConfiguration类存在.该类在Commons Configuration中充当大多数Con ...
- 计算java对象的内存占用
代码引用自:https://blog.csdn.net/antony9118/article/details/54317637 感谢博主分享: import java.util.ArrayList; ...
- input[type="file"]的样式以及文件名的显示
如何美化input[type="file"] 基本思路是: (1)首先在 input 外层套一个 div : (2)将 div 和 input 设置为一样大小(width和heig ...
- 【cs231n】神经网络学习笔记1
神经网络推荐博客: 深度学习概述 神经网络基础之逻辑回归 神经网络基础之Python与向量化 浅层神经网络 深层神经网络 前言 首先声明,以下内容绝大部分转自知乎智能单元,他们将官方学习笔记进行了很专 ...
- spring boot 使用拦截器,注解 实现 权限过滤
http://www.cnblogs.com/zhangXingSheng/p/7744997.html spring boot 使用拦截器 实现 用户登录拦截 http://www.cnblogs. ...
- codeforces600E Lomsat gelral
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- APP Inventor 基于网络微服务器的即时通信APP
APP Inventor 基于网络微服务器的即时通信APP 一.总结 一句话总结:(超低配版的QQ,逃~) 1.APP Inventor是什么? google 傻瓜式 编程 手机 app App In ...