Python Small instance one: simple Reptile __python

Source: Internet
Author: User

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 ')

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.