目标:  


  • 首先肯定要实现图片抓取这个基本功能
  • 然后实现对用户所给的链接进行抓取
  • 最后要有一定的交互,程序不能太傻吧


一、页面获取

要让python可以进行对网页的访问,那肯定要用到urllib之类的包。So先来个 import urllib.request 

urllib中有 urllib.request.urlopen(str) 方法用于打开网页并返回一个对象,调用这个对象的read()方法后能直接获得网页的源代码,内容与浏览器右键查看源码的内容一样。


1 #coding:utf-8
2 import urllib.request
3
4 page = urllib.request.urlopen('http://tieba.baidu.com/p/1753935195')#打开网页
5 htmlcode = page.read()#读取页面源码
6 print htmlcode#在控制台输出


运行结果与查看源码其实差不多

运行结果就不放上来了

也可以写到文本文档中:


#coding:utf-8

import urllib.request

page = urllib.request.urlopen('http://tieba.baidu.com/p/1753935195')#打开网页

htmlcode = page.read()#读取页面源码

#print(htmlcode)#在控制台输出

pageFile = open('C:\pageCode.txt','wb+')#以写的方式打开pageCode.txt

pageFile.write(htmlcode)#写入

pageFile.close()#开了记得关

运行一遍,txt就出现在了getJpg.py【爬虫】Python爬虫01——第一个小爬虫【修正版】_字符串的目录下

好了别闹,我们把它封装成方法:


def get_html(url):

     page = urllib.request.urlopen(url)

     html = page.read()

     return html

然后我们的页面获取代码就K.O.

二、图片(目标)的提取

做完上面步骤,你打开txt一看,我去!这都是什么跟什么啊,根本找不到图片在哪好伐?

客官别急啊,我这就去给你叫我们的小。。。图片!图片!

首先我们要一个正则表达式 (什么你不会?请看菜鸟入门教程-->​Go​)

然后我们看源代码,Yeah 我们找到了其中一张图片是这样的​

写出图片的正则表达式: reg = r'src="(.+?\.jpg)" width' 

解释下吧——匹配以src="开头然后接一个或多个任意字符(非贪婪),以.jpg" width结尾的字符串。比如图中红框内src后 双引号里的链接就是一个匹配的字符串。

接着我们要做的就是从get_html方法返回的辣么长一串字符串中 拿到 满足正则表达式的 字符串。

用到python中的re库中的 re.findall(str) 它返回一个满足匹配的字符串组成的列表


 Python Code 



1
2
3
4
5
6
7
8
9
10
11




def get_html(url):
     page = urllib.request.urlopen(url)
     html = page. read()
      return html
reg = r 'src="(.+?\.jpg)" width' #正则表达式
reg_img = re. compile(reg) #编译一下,运行更快
html = get_html( 'http://tieba.baidu.com/p/1753935195')
html=html. decode( 'utf-8') #python3
imglist = reg_img.findall(html) #进行匹配
for img  in imglist:
     print(img)



打印出这么多图片链接


https://imgsa.baidu.com/forum/w=580/sign=92f7a739462309f7e76fad1a420f0c39/7eba40ed2e738bd46ea1009ca18b87d6267ff9ae.jpg
https://imgsa.baidu.com/forum/w=580/sign=71ef1fbf720e0cf3a0f74ef33a47f23d/17128d01a18b87d6c6c219b2070828381e30fdae.jpg
https://imgsa.baidu.com/forum/w=580/sign=63deb3355edf8db1bc2e7c6c3922dddb/74ac06f431adcbef49b7ccabacaf2edda2cc9fae.jpg
https://imgsa.baidu.com/forum/w=580/sign=8bc12c85972bd40742c7d3f54b889e9c/59114b34970a304ea7fa1257d1c8a786c8175c55.jpg
https://imgsa.baidu.com/forum/w=580/sign=c930608c90ef76c6d0d2fb23ad14fdf6/9e89e350352ac65c3cd5d4defbf2b21192138a5d.jpg
https://imgsa.baidu.com/forum/w=580/sign=e580d266b2fb43161a1f7a7210a64642/1715ba51f8198618ee975d



光把链接拿出来没用啊,我们的目标是下载下来~
urllib库中有一个 urllib.request.urlretrieve(链接,名字) 方法,它的作用是以第二个参数为名字下载链接中的内容,我们来试用一下
在上面代码循环中加上 urllib.request.urlretrieve(img, 'tieba.jpg')


 Python Code 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17




import urllib.request
import re
 
def get_html(url):
    page = urllib.request.urlopen(url)
    html = page. read()
     return html
 
reg = r 'src="(.+?\.jpg)" width'
reg_img = re. compile(reg)
html = get_html( 'http://tieba.baidu.com/p/1753935195')
html=html. decode( 'utf-8') #python3
imglist = reg_img.findall(html) #进行匹配
x =  0
for img  in imglist:
    urllib.request.urlretrieve(img,  'C:\img\%s.jpg' %x)
    x +=  1



第一步完成~




三、指定链接抓取


我想要抓另一个帖子,总不能打开源代码,然后把那段地址改了在运行吧。

只是一个小程序,那也不行欸,加一个让用户指定地址的交互。

先把提取图片的那段代码打包下:


 Python Code 



1
2
3
4
5
6
7
8
9




def get_image(html_code):
    reg = r 'src="(.+?\.jpg)" width'
    reg_img = re. compile(reg)
    img_list = reg_img.findall(html_code)
    x =  0
     for img  in img_list:
        urllib.request.urlretrieve(img,  'C:\img\%s.jpg' %x)
        x +=  1








最后来个请输入:

 Python Code 



1
2
3
4
5
6
7
8




print(u '请输入url:')
url =  input()
if url:
      pass
else:
    url =  'http://tieba.baidu.com/p/1753935195'
html_code = get_html(url)
get_image(html_code)




四、交互的添加

虽然写的是一个简单的小程序,但有强迫症的我还是给他加上了交互(不然多难受啊:双击,屏幕一闪,下载完了。。。)


最后的代码


# coding:utf-8

import urllib.request

import re


def get_html(url):

    page = urllib.request.urlopen(url)

    html = page.read()

    return html


def get_image(html_code):

    reg = r'src="(.+?\.jpg)" width'

    reg_img = re.compile(reg)

    img_list = reg_img.findall(html_code)

    x = 0

    for img in img_list:

        urllib.request.urlretrieve(img, 'C:\img\%s.jpg' %x)

        x += 1


print(u'-------网页图片抓取-------')

print(u'请输入url:')

url = input()

if url:

    pass

else:

    print(u'---没有地址输入正在使用默认地址---')

    url = 'http://tieba.baidu.com/p/1753935195'

print(u'----------正在获取网页---------')

html_code = get_html(url)

print(u'----------正在下载图片---------')

get_image(html_code)

print(u'-----------下载成功-----------')

 

input('Press Enter to exit')