This article is called the crawler through the local remote access to the URL, and then read the URL into the source code form, and then the source code analysis, to obtain their own required data, the equivalent of simple data mining. This article is the implementation of the image of a Web page to crawl out of the process of saving to the local, the example is very simple, with the Python 3.5.2 version, the previous version may import the package name is not the same, the call library function way somewhat different. The code is as follows:
#coding =utf-8
Import urllib.request
import re
def gethtml (URL):
page = urllib.request.urlopen (URL) # #打开页面
html = page.read () # #获取目标页面的源码 return
HTML
def getimg (HTML):
reg = ' src= ' (. +?\.png) "' # #正则表达式筛选目标图片格式, some are ' data-original= ' (. +?\.jpg) "'
img = re.compile (reg)
html = html.decode (' Utf-8 ') # #编码方式为utf-8
imglist = Re.findall (img, HTML) # #解析页面源码获取图片列表
#print (imglist)
x = 0
#length = Len (imglist) for
I in range (6): # #取前6张图片保存
imgurl = imglist[i]
#imgurl = Re.sub (' "(. *?)" ', R ' \1 ', Imgurl) #取单引号里的双引号内容
#print (imgurl)
urllib.request.urlretrieve (Imgurl, '%s.jpg '% x) # #将图片从远程下载到本地并保存
x + + 1
global max_num
max_num = 1
# #有时候无法打开目标网页, it needs to be tried multiple times, set to 1 times for I in
Range (max_num):
try:
html = gethtml ("view-source:http://www.shangxueba.com/jingyan/2438398.html")
getimg (HTML)
break
except:
If I < max_num-1:
continue
Else:
print (' Urlerror: <urlopen error Timed out> All times are failed ')