我的第一个爬虫

来源:互联网 发布:全国乡镇经纬度数据库 编辑:程序博客网 时间:2024/06/04 19:12
# 环境python 3.5.2 + beautifulsoup  
# 准备工作:了解 python 自带的模块 urllib + beautifulsoup 的网页解析
#爬取了一个图片网站的部分图片
from urllib import requestfrom bs4 import BeautifulSoupimport osimport os.pathimport retry:response = request.urlopen('https://www.4493.com/weimeixiezhen/')       #使用urlopen方法下载网页soup = BeautifulSoup(response,'html.parser', from_encoding = 'utf-8')print("来自源URL要爬取的所有网页")links = soup.find_all('a', href = re.compile(r"/weimeixiezhen/120"))count = 1                                                               # 记录下载的图片数目for link in links:urls = link['href']print(urls)url = 'https://www.4493.com'+urlsprint("具体网页: ", url)try:response_1 = request.urlopen(url)soup_1 = BeautifulSoup(response_1,'html.parser', from_encoding = 'utf-8')img = soup_1.find('img', onload = re.compile(r"btn"))except request.HTTPError as e:print(e.code)except request.URLError as e:print(e.reason)p = os.path.join('E:\\', "MV_img")if not os.path.exists(p):                                         # 判断当前目录是否存在os.mkdir(p)                                                   # 如果存在,才创建新的目录url = img['src']                                                  # 获取图片的资源链接urlrequest.urlretrieve(url, 'E:\\MV_img\\MM_%s.jpg' %count)          # 根据图片的资源链接下载图片print('第%d张图片下载完成'%count)count += 1except request.HTTPError as e :print(e.code)except request.URLError as e :print(e.reason)else:print("ok")


原创粉丝点击