python--(爬虫-re模块)
python--(爬虫-re模块)
re模块四大核心功能:
1.findall 查找所有,返回list
import re
lst = re.findall("m", "mai le fo len, mai ni mei!")
print(lst) # ['m', 'm', 'm'] lst = re.findall(r"\d+", "5点之前. 你要给我5000")
print(lst) # ['5' '5000']
2.search 会进行匹配,但如果匹配到了第一个结果,就会返回这个结果,
如果匹配不上search返回的则是None
import re
ret = re.search(r'\d', '5点之前. 你要给我5000万').group()
print(ret) #
3. match 只能从字符串的开头进⾏匹配
import re
ret = re.match('a', 'abc').group()
print(ret) # a
4. finditer 和findall差不多. 只不过这时返回的是迭代器
import re
it = re.finditer("m", "mai le fo len, mai ni mei!")
for el in it:
print(el.group()) # 依然需要分组
5.re模块相关操作
import re # split 切割. 按照正则切割.
# lst = re.split(r"[ab]", "abcdefghahahehedebade")
# print(lst) # sub 替换.
# result = re.sub("250", "__sb__", "alex250taibai250taihei250ritian250liuwei")
# print(result) # result = re.subn("250", "__sb__", "alex250taibai250taihei250ritian250liuwei")
# print(result) # obj = re.compile(r"\d+")
# lst = obj.findall("大阳哥昨天赚了5000块")
# lst2 = obj.findall("银行流水5000, 花了6000")
# print(lst)
# print(lst2) # obj = re.compile(r"(?P<id>\d+)(?P<zimu>e{3})")
# ret = obj.search("abcdefg123456eeeee") # ((123456)(eee))
# print(ret.group())
# print(ret.group("id"))
# print(ret.group("zimu")) # ret = re.findall('www.(baidu|oldboy).com', 'www.oldboy.com')
# print(ret) # 这是因为findall会优先把匹配结果组⾥内容返回,如果想要匹配结果,取消权限即可
# ret = re.findall('www.(?:baidu|oldboy).com', 'www.oldboy.com') # ?: 当前的()不分组
# print(ret) # ['www.oldboy.com'] # ret=re.split("sb","alexsbwusirsbtaibaisbliuwei")
# print(ret)
爬虫重点:爬取豆瓣网站相关信息===>
import re
from urllib.request import urlopen # 打开一个链接. 读取源代码
import ssl
# 干掉数字签名证书
ssl._create_default_https_context = ssl._create_unverified_context def getPage(url):
response = urlopen(url) # 和网页链接
return response.read().decode('utf-8') # 返回正常的页面源代码. 一大堆HTML
def parsePage(s): # s 是页面源代码
ret = re.findall('<div class="item">.*?<div class="pic">.*?<em .*?>(?P<id>\d+).*?'+
'<span class="title">(?P<title>.*?)</span>'+
'.*?<span class="rating_num" .*?>(?P<rating_num>.*?)</span>.*?<span>'+
'(?P<comment_num>.*?)评价</span>', s, re.S)
return ret # id,title, rating_num, comment_num def main(num):
url = 'https://movie.douban.com/top250?start=%s&filter=' % num
response_html = getPage(url) # response_html是页面源代码
ret = parsePage(response_html)
print(ret) # id,title, rating_num, comment_num count = 0
for i in range(10): #
main(count)
count += 25
方法一
import re
from urllib.request import urlopen # 打开一个链接. 读取源代码
import ssl
# 干掉数字签名证书
ssl._create_default_https_context = ssl._create_unverified_context def getPage(url):
response = urlopen(url) # 和网页链接
return response.read().decode('utf-8') # 返回正常的页面源代码. 一大堆HTML def parsePage(s):
com = re.compile(
'<div class="item">.*?<div class="pic">.*?<em .*?>(?P<id>\d+).*?' +
'<span class="title">(?P<title>.*?)</span>' +
'.*?<span class="rating_num" .*?>(?P<rating_num>.*?)</span>.*?<span>' +
'(?P<comment_num>.*?)评价</span>', re.S)
ret = com.finditer(s)
for i in ret:
yield {
"id": i.group("id"),
"title": i.group("title"),
"rating_num": i.group("rating_num"),
"comment_num": i.group("comment_num"),
} def main(num):
url = 'https://movie.douban.com/top250?start=%s&filter='
response_html = getPage(url)
print(response_html)
ret = parsePage(response_html)
# print(ret)
f = open("move_info7", "a", encoding="utf8")
for obj in ret:
print(obj)
data = str(obj)
f.write(data + "\n") count = 0
for i in range(10): #
main(count)
count += 25
爬取并写入文件
python--(爬虫-re模块)的更多相关文章
- python爬虫 urllib模块url编码处理
案例:爬取使用搜狗根据指定词条搜索到的页面数据(例如爬取词条为‘周杰伦'的页面数据) import urllib.request # 1.指定url url = 'https://www.sogou. ...
- python 爬虫 urllib模块 目录
python 爬虫 urllib模块介绍 python 爬虫 urllib模块 url编码处理 python 爬虫 urllib模块 反爬虫机制UA python 爬虫 urllib模块 发起post ...
- Python爬虫urllib模块
Python爬虫练习(urllib模块) 关注公众号"轻松学编程"了解更多. 1.获取百度首页数据 流程:a.设置请求地址 b.设置请求时间 c.获取响应(对响应进行解码) ''' ...
- python爬虫-urllib模块
urllib 模块是一个高级的 web 交流库,其核心功能就是模仿web浏览器等客户端,去请求相应的资源,并返回一个类文件对象.urllib 支持各种 web 协议,例如:HTTP.FTP.Gophe ...
- Python爬虫——selenium模块
selenium模块介绍 selenium最初是一个测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题 selenium本质是通过驱动浏览器,完全模拟浏览 ...
- python 爬虫 urllib模块介绍
一.urllib库 概念:urllib是Python自带的一个用于爬虫的库,其主要作用就是可以通过代码模拟浏览器发送请求.其常被用到的子模块在Python3中的为urllib.request和urll ...
- python爬虫--selenium模块.上来自己动!
selenium 基本操作 from selenium import webdriver from time import sleep #实例化一个浏览器对象 bro = webdriver.Chro ...
- python 爬虫 urllib模块 反爬虫机制UA
方法: 使用urlencode函数 urllib.request.urlopen() import urllib.request import urllib.parse url = 'https:// ...
- python爬虫-smtplib模块发送邮件
1.代码如下: import smtplib from email.message from EmailMessage # smtplib模块负责发送邮件服务 # email.message模块负责构 ...
- Python爬虫常用模块,BeautifulSoup笔记
import urllib import urllib.request as request import re from bs4 import * #url = 'http://zh.house.q ...
随机推荐
- ansible --help 文档
> ansible --help Usage: ansible <host-pattern> [options] Define and run a single task 'play ...
- Django 中Admin站点的配置
Admin站点是django提供的一个后台管理页面,可以用来对用户与数据库表数据进行管理. Admin站点配置流程 1.在settings.py文件中INSTALL_APPS列表中添加django.c ...
- vue 上传图片到阿里云(前端直传:不推荐)
为何要这样做:减轻后端数据库压力(个人觉得于前端没啥用,谁返回来都行) 代码部分: <template> <div class="upLoad"> < ...
- linux 编译网卡驱动
将smsc7500网卡驱动拷贝到/drive/net/usb文件夹下 拷贝ioctl_7500.h smsc7500usbnet.c smsc7500version.h smsclan7500.h ...
- python pymysql存储
# _*_ coding:utf-8 _*_ import requests from bs4 import BeautifulSoup import re import pymysql def cr ...
- @Autowired @Resource @Inject 自动注入
一.@AutoWired ( spring 的注解 )自动注入 /** * @Autowired: * 默认按照 Student 类型去容器中找对应的组件:applicationContext.get ...
- cocos2d-x 3.0 经常使用对象的创建方式
cocos2d-x 3.0 中全部对象差点儿都能够用create函数来创建,其它的创建方式也是有create函数衍生. 以下来介绍下create函数创建一般对象的方法,省得开发中常常忘记啥的. 1.精 ...
- Mac OSX Yosemite 10.10 brew 错误:mktemp: mkdtemp failed on /tmp/git-LIPo: No such file or directory
这个问题困扰了我非常久非常久.使得我不得不花一点时间来说一下解决方法. 事情是这种:前两天兴高採烈的更新了一下宝贝mac到10.10. 一切看起来都那么美好,可是. .当我又一次安装magento的时 ...
- Chrome下使用百度地图报错Cannot read property 'minZoom' of undefined
问题:工作中在Google chome下面的js console里面测试百度地图API var map = new BMap.Map("container"); map.cente ...
- 0x18 总结与练习
这一章不太满意啊.. 还是有点痛苦,但就是做的挺慢啊... 1.就是例题 2.括号画家 感觉这种提高组类型的细节题都没什么信心啊,fail了几次才A #include<cstdio> #i ...