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正则表达式,正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的 一些特定字符,及这些特定字符的组合,组成一个"规则字符串",然后用 ...
随机推荐
- shell 简单脚本 2
#!/bin/bash source /etc/profile APPLICATIONS_HOME="/cpic/cpicapp/cpic_analy/jars" APPLICAT ...
- winform中让显示的图片覆盖到父窗体保持父窗体的不可选中的状态,且任务栏中不会显示子窗体的任务选项
要求:为父窗体添加一个类似于加载等待的功能,当窗体点击备份时弹出且覆盖掉窗体 问题一产生:当为弹窗添加控件时,form.show();导致窗体卡死,控件变得透明化; 问题一分析:当窗体show();之 ...
- this+call、apply、bind的区别与使用
http://www.ruanyifeng.com/blog/2018/06/javascript-this.html https://segmentfault.com/a/1190000018017 ...
- 2、Task 使用 ContinueWith 而不要使用 Wait
1.线程自旋:在阻塞线程的时候为了等待解锁(访问临界资源)(Sleep). 2.上下文切换:将处理器当前线程的状态保存到操作系统内部的线程对象中,然后再挑出一个就绪的线程,把上下文信息传递给处理器,然 ...
- java中regex参考
在Sun的Java JDK 1.40版本中,Java自带了支持正则表达式的包,本文就抛砖引玉地介绍了如何使用java.util.regex包. 可粗略估计一下,除了偶尔用Linux的外,其他Linu ...
- 九:SQL之DQL数据查询语言多表操作
前言: 一:数据准备 员工表emp 和部门表 dept 注意:我在录入员工表的时候,特意添加了两条没有部门的员工,他们的部门id对应为null; --分别创建部门和员工表,并实现一对多关系 DROP ...
- 蓝牙学习(4) -- L2CAP
L2CAP in protocol 首先看一下L2CAP在Bluetooth protocol architecture diagram中的位置: Features of L2CAP Logical ...
- (转) 苹果所有常用证书,appID,Provisioning Profiles配置说明及制作图文教程(精)
原文地址:http://blog.csdn.net/holydancer/article/details/9219333 概述: 苹果的证书繁锁复杂,制作管理相当麻烦,今天决定重置一个游戏项目中的所有 ...
- LeetCode(108) Convert Sorted Array to Binary Search Tree
题目 Given an array where elements are sorted in ascending order, convert it to a height balanced BST. ...
- JavaScript正则表达式-RegExp对象
RegExp对象方法 exec():与String对象的match()方法功能相同. 参数为被搜索字符串.返回数组或null. test():与String对象的search()方法功能相同. 参数为 ...