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. python实现微信打飞机游戏(by crossin)

    # -*- coding: utf-8 -*- import pygame from sys import exit import random pygame.init() screen = pyga ...

  2. Unity之脚本编译顺序

    根据官方的解释,它们的编译顺序如下: (1)所有在Standard Assets.Pro Standard Assets或者Plugins文件夹中的脚本会产生一个Assembly-CSharp-fil ...

  3. UVA 11374 Airport Express (最短路)

    题目只有一条路径会发生改变. 常见的思路,预处理出S和T的两个单源最短路,然后枚举商业线,商业线两端一定是选择到s和t的最短路. 路径输出可以在求最短路的同时保存pa数组得到一棵最短路树,也可以用di ...

  4. Educational Codeforces Round 12补题 经典题 再次爆零

    发生了好多事情 再加上昨晚教育场的爆零 ..真的烦 题目链接 A题经典题 这个题我一开始推公式wa 其实一看到数据范围 就算遍历也OK 存在的问题进制错误 .. 思路不清晰 两个线段有交叉 并不是端点 ...

  5. git快速入门(MAC系统,github,ssh key)

    如果使用过svn的话,git大致可以认为是多了本地库的svn.git先本地提交commit到本地库,然后再push到远程服务器的库.git是分布式的代码管理工具,基于SSH协议.ssh的作用就是为了不 ...

  6. ajax的traditional属性

    jquery框架的ajax参数除了常用的 $.ajax({ url: 'xxx', type: 'xxx', data: 'xxx', success: 'xxx' ... }) 外还有一个参数需要特 ...

  7. k8s学习目录

    目录 K8S基础部分 基础部分 5 秒创建 k8s 集群[转] k8s 核心功能[转] k8s 重要概念[转] 部署 k8s Cluster(上)[转] 部署 k8s Cluster(下)[转] Ku ...

  8. How to debug add-ins for arcgis

    Debugging add-ins To debug an add-in, follow these steps: Confirm that the add-in is deployed to the ...

  9. 第2节 azkaban调度:1、azkaban的调度任务使用

    2.4 Azkaban实战 Azkaba内置的任务类型支持command.java Command类型单一job示例 创建job描述文件 创建文本文件,更改名称为mycommand.job 注意后缀. ...

  10. flume启动报错

    执行flume-ng agent -c conf -f conf/load_balancer_server.conf -n a1 -Dflume.root.logger=DEBUG,console , ...