python简易爬虫,帮助理解re模块
20161203更新:
1.使用了BS4解析html
2.使用了mysql-connector插入了数据库表
pip install mysql-connector
import urllib.request
from bs4 import BeautifulSoup
import re
import mysql.connector def getMovieInfo():
url="https://movie.douban.com"
data=urllib.request.urlopen(url).read()
page_data=data.decode('UTF-8')
'''''print(page_data)'''
soup=BeautifulSoup(page_data,"html.parser") #连接mysql
conn = mysql.connector.connect(host='locahost',user='root',password='',database='test')
cursor = conn.cursor()
cursor.execute('delete from doubanmovie where 1=1') for link in soup.findAll('li',attrs={"data-actors": True}):
moviename=link['data-title']
actors = link['data-actors']
region=link['data-region']
release=link['data-release']
duration = link['data-duration']
director = link['data-director']
rate = link['data-rate']
imgsrc =link.img['src'] cursor.execute("INSERT INTO doubanmovie VALUES ('', %s, %s, %s, %s, %s, %s, %s, %s,now())",[moviename,actors,region,release,duration,director,rate,imgsrc]) conn.commit() print('mysql',cursor.rowcount)
print(link['data-title'])
print('演员:',link['data-actors'])
print(link.img['src'])
cursor.close()
conn.close() #函数调用
getMovieInfo()
更新:基于python3的爬虫教程
两个版本代码区别:
1.在3中,urllib.urlopen变成urllib.request.urlopen,之前的都要加request
2.在3中,print后面要加(),即输出代码:print()
3.在3中,
html = urllib.request.urlopen(url).read()返回的是byte类型,字节码,需要转换成UTF-8,
代码:html = html.decode('utf-8')
#coding=utf-8
import urllib.request
import re def getHtml(url):
page = urllib.request.urlopen(url)
html = page.read()
html =html.decode('utf-8')
return html def getImg(html):
reg = r'src="(.+?\.jpg)" pic_ext'
imgre = re.compile(reg)
imglist = re.findall(imgre,html)
x = 0
for imgurl in imglist:
urllib.request.urlretrieve(imgurl,'%s.jpg' % x)
x+=1 html = getHtml("http://tieba.baidu.com/p/2460150866") print (getImg(html))
以下是基于python2的:
把筛选的图片地址通过for循环遍历并保存到本地,代码如下:

#coding=utf-8
import urllib
import re def getHtml(url):
page = urllib.urlopen(url)
html = page.read()
return html def getImg(html):
reg = r'src="(.+?\.jpg)" pic_ext'
imgre = re.compile(reg)
imglist = re.findall(imgre,html)
x = 0
for imgurl in imglist:
urllib.urlretrieve(imgurl,'%s.jpg' % x)
x+=1 html = getHtml("http://tieba.baidu.com/p/2460150866") print getImg(html)

这里的核心是用到了urllib.urlretrieve()方法,直接将远程数据下载到本地。
我们又创建了getImg()函数,用于在获取的整个页面中筛选需要的图片连接。re模块主要包含了正则表达式:
re.compile() 可以把正则表达式编译成一个正则表达式对象.
re.findall() 方法读取html 中包含 imgre(正则表达式)的数据。
运行脚本将得到整个页面中包含图片的URL地址。
python简易爬虫,帮助理解re模块的更多相关文章
- python简易爬虫来实现自动图片下载
菜鸟新人刚刚入住博客园,先发个之前写的简易爬虫的实现吧,水平有限请轻喷. 估计利用python实现爬虫的程序网上已经有太多了,不过新人用来练手学习python确实是个不错的选择.本人借鉴网上的部分实现 ...
- 爬虫系列1:python简易爬虫分析
决定写一个小的爬虫系列,本文是第一篇,讲爬虫的基本原理和简易示例. 1.单个网页的简易爬虫 以下爬虫的主要功能是爬取百度贴吧中某一页面的所有图片.代码由主要有两个函数:其中getHtml()通过页面u ...
- python网络爬虫之二requests模块
requests http请求库 requests是基于python内置的urllib3来编写的,它比urllib更加方便,特别是在添加headers, post请求,以及cookies的设置上,处理 ...
- Python简易爬虫
经常需要下载论文,每次都需要去网页上搜索,然后点击下载,实在麻烦,正好最近刚入门Python,心血来潮,想着写一个爬虫 经过一天查阅资料,基本算是完成了,但是还是不足,比如对知网和万方暂时还不行,但是 ...
- Python简易爬虫爬取百度贴吧图片
通过python 来实现这样一个简单的爬虫功能,把我们想要的图片爬取到本地.(Python版本为3.6.0) 一.获取整个页面数据 def getHtml(url): page=urllib.requ ...
- Python之爬虫的理解
# -*- coding: utf-8 -*- 中文用户一定先用这行来声明编码方式 爬虫: 爬虫是自动访问互联网,并且提取数据的程序 (从网络上获取非结构化的数据,ETL将这些数据转换为结构化数 ...
- 【Python】Python简易爬虫爬取百度贴吧图片
通过python 来实现这样一个简单的爬虫功能,把我们想要的图片爬取到本地.(Python版本为3.6.0) 一.获取整个页面数据 def getHtml(url): page=urllib.requ ...
- python简易爬虫实现
目的:爬取昵称 目标网站:糗事百科 依赖的库文件:request.sys.beautifulSoup4.imp.io Python使用版本:3.4 说明:参考http://cn.python-requ ...
- python网络爬虫之三re正则表达式模块
""" re正则表达式,正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的 一些特定字符,及这些特定字符的组合,组成一个"规则字符串",然后用 ...
随机推荐
- UISearchBar clearButton
When the searchBar:textDidChange: method of the UISearchBarDelegate gets called because of the user ...
- 10048 - Audiophobia (Floyd)
Floyd的变形,本质是动态规划,路径分成的两个部分中取最大值作为该路径的答案,在所有可行路径之中选一个最小值. #include<bits/stdc++.h> using namespa ...
- UVA 753 A Plug for UNIX (最大流)
关键在建图,转换器连一条容量无限的边表示可以转化无数次,设备的插头连源点,插座连汇点. dinic手敲已熟练,输出格式又被坑,总结一下,输出空行多case的,一个换行是必要的,最后一个不加空行,有Te ...
- solr scheme配置简介
solr 字段配置,和数据库数据索引配置 配置solr字段. schema.xml 文件里配置 先讲解一下,里面的一些字段 1. <types> ... </types> 表示 ...
- 解决因为手机设置字体大小导致h5页面在webview中变形的BUG
首先,我们做了一个H5页面,在各种手机浏览器中打开都没问题.我们采用了rem单位进行布局,通过JS来动态计算网页的视窗宽度,动态设置html的font-size,一切都比较完美. 这时候,你自信满满的 ...
- mysql利用binlog恢复数据详细例子
模拟数据恢复的案例 有些时候脑瓜就会短路,难免会出错 场景:在生产环境中,我们搭建了mysql主从,备份操作都是在从备份数据库上 前提:有最近一天或者最近的全备 或者最近一天相关数据库的备份 最重要的 ...
- const、let、var的区别
const不能从字面上来理解,他不能修改的是栈内存在的值和地址. 使用const声明的是常量,在后面出现的代码中不能再修改该常量的值. 怎么理解栈内存在的值和地址呢?就要从javascript的类型说 ...
- python-DB模块实例
MySQLdb其实有点像php或asp中连接数据库的一个模式了,只是MySQLdb是针对mysql连接了接口,我们可以在python中连接MySQLdb来实现数据的各种操作. python连接mysq ...
- graphviz 布局和子图,表格教程
有了这三个利器,就搞定架构图了. 子图间互相调用要开启 http://graphviz.org/pdf/dotguide.pdf
- ios retain copy 以及copy协议
阅读本文之前首先了解Copy与Retain的区别: Copy是创建一个新对象,Retain是创建一个指针,引用对象计数加1. Copy属性表示两个对象内容相同,新的对象retain为1 ,与旧有对象的 ...