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模块的更多相关文章

  1. python简易爬虫来实现自动图片下载

    菜鸟新人刚刚入住博客园,先发个之前写的简易爬虫的实现吧,水平有限请轻喷. 估计利用python实现爬虫的程序网上已经有太多了,不过新人用来练手学习python确实是个不错的选择.本人借鉴网上的部分实现 ...

  2. 爬虫系列1:python简易爬虫分析

    决定写一个小的爬虫系列,本文是第一篇,讲爬虫的基本原理和简易示例. 1.单个网页的简易爬虫 以下爬虫的主要功能是爬取百度贴吧中某一页面的所有图片.代码由主要有两个函数:其中getHtml()通过页面u ...

  3. python网络爬虫之二requests模块

    requests http请求库 requests是基于python内置的urllib3来编写的,它比urllib更加方便,特别是在添加headers, post请求,以及cookies的设置上,处理 ...

  4. Python简易爬虫

    经常需要下载论文,每次都需要去网页上搜索,然后点击下载,实在麻烦,正好最近刚入门Python,心血来潮,想着写一个爬虫 经过一天查阅资料,基本算是完成了,但是还是不足,比如对知网和万方暂时还不行,但是 ...

  5. Python简易爬虫爬取百度贴吧图片

    通过python 来实现这样一个简单的爬虫功能,把我们想要的图片爬取到本地.(Python版本为3.6.0) 一.获取整个页面数据 def getHtml(url): page=urllib.requ ...

  6. Python之爬虫的理解

    #  -*- coding: utf-8 -*-  中文用户一定先用这行来声明编码方式 爬虫: 爬虫是自动访问互联网,并且提取数据的程序  (从网络上获取非结构化的数据,ETL将这些数据转换为结构化数 ...

  7. 【Python】Python简易爬虫爬取百度贴吧图片

    通过python 来实现这样一个简单的爬虫功能,把我们想要的图片爬取到本地.(Python版本为3.6.0) 一.获取整个页面数据 def getHtml(url): page=urllib.requ ...

  8. python简易爬虫实现

    目的:爬取昵称 目标网站:糗事百科 依赖的库文件:request.sys.beautifulSoup4.imp.io Python使用版本:3.4 说明:参考http://cn.python-requ ...

  9. python网络爬虫之三re正则表达式模块

    """ re正则表达式,正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的 一些特定字符,及这些特定字符的组合,组成一个"规则字符串",然后用 ...

随机推荐

  1. Nodejs + Jshint自动化静态代码检查

    1.   目的 提交代码前能够自动化静态代码检查,提高代码质量 2.   准备 1.    Nodejs安装: 官方地址:http://nodejs.org/ 安装说明:根据电脑配置下载对应的版本进行 ...

  2. openstack 存储节点按照报错Device /dev/sdb not found (or ignored by filtering).

    root@dell-PowerEdge-T30:~# pvcreate /dev/sdb  Device /dev/sdb not found (or ignored by filtering).首页 ...

  3. Linux下文件以及文件名编码转换

    1.查看文件编码方式--file 文件名(但不是很准确) yang@mint-linux ~ $ file baidu.html baidu.html: HTML document, UTF-8 Un ...

  4. 如何在ABAP里用函数式编程思想打印出非波拉契Fibonacci(数列)

    在JavaScript里可以用ES6提供的FunctionGenerator这种黑科技来打印非波拉契数列,具体细节参考我这篇文章. 在ABAP里也有很多种方式实现这个需求. 下面这个report分别用 ...

  5. UIButton 左对齐 省略号最右边

    //左对齐 [_btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft]; //省略号靠右侧 _btn.ti ...

  6. pc端引入微信公众号文章

    最近做了一个小需求,结果坑特别多..... 需求是这样的,要给公司内部做一个微信公众号广告投票系统,整个项目就不多赘述了,有个小功能,要求是这样的: 点击某条记录后的“投票”按钮,在当前页面弹出弹窗显 ...

  7. 汇编segment

    一个正常的应用程序被由若干个 segment组成. 定义 segment: SECTION .段名 SECTION 也可以小写 如: 定义数据段: section .data   定义代码段: sec ...

  8. linux or msys2设置网络代理

    在文件 .bashrc 中添加 export http_proxy="proxy IP:port" 如 export http_proxy="192.168.0.1:80 ...

  9. ZOJ Monthly, January 2019-Little Sub and Pascal's Triangle

    这个题的话,它每行奇数的个数等于该行行号,如果是0开始的,就该数的二进制中的1的个数,设为k,以它作为次数,2k就是了. #include <stdio.h> int main() { i ...

  10. (8)zabbix监控项item是什么

    什么是item Items是从主机里面获取的所有数据.通常情况下我叫itme为监控项,例如服务器加入了zabbix监控,我需要监控它的cpu负载,那么实现这个方法的东西就叫item item构成 it ...