[python]利用BeautifulSoup进行简单图片抓取

需要事先安装好BeautifulSoup,官网:https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html

抓取地址:http://tieba.baidu.com/p/2772656630


代码

#-*- coding:utf-8 -*-

import urllib
from bs4 import BeautifulSoup

def get_content(url):
	
	html = urllib.urlopen(url)
	content = html.read()
	html.close()

	return content

def get_images(content):
	
	oSoup = BeautifulSoup(content)
	
	all_images = oSoup.find_all('img', class_="BDE_Image")

	x = 1
	for img in all_images:
		print img['src']
		image_name = "%s.jpg" % x

		urllib.urlretrieve(img['src'], image_name)
		x+=1
		

url = "http://tieba.baidu.com/p/2772656630"	

content = get_content(url)

get_images(content)




你可能感兴趣的:(python,爬虫)