博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python爬虫-爬取你想要的小姐姐
阅读量:4550 次
发布时间:2019-06-08

本文共 1919 字,大约阅读时间需要 6 分钟。

一、准备

1. 原地址

2. 检查html发现,网页是有规则的分页, 最大图片的class为pic-large

 

二、代码

1 import requests 2 import os 3 from bs4 import BeautifulSoup 4  5 url = 'http://www.win4000.com/wallpaper_detail_157712.html' 6 imgmkdir = 'D://Download//ghost_1//' 7  8  9 # 获取网页url10 def getUrlList():11     imgUrlList = []12     for i in range(0, 10):13         imgUrl = ''14         url_split = url.split('.html')15         if not i == 0:16             imgUrl += url_split[0] + '_' + str(i) + '.html'17             # print(imgUrl)18             imgUrlList.append(imgUrl)19 20     return imgUrlList21 22 23 # 下载图片24 def downImg(imgUrl):25     try:26         if not os.path.exists(imgmkdir):27             os.mkdir(imgmkdir)28         if not os.path.exists(imgUrl):29             r = requests.get(imgUrl)30             r.raise_for_status()31             # 使用with语句可以不用自己手动关闭已经打开的文件流32             imgpath = imgmkdir + imgUrl.split('/')[-1]33             # 开始写文件, wb表示写二进制文件34             with open(imgpath, 'wb') as f:35                 f.write(r.content)36             print(imgUrl + '【爬取完成】')37         else:38             print(imgUrl.split('/')[-1] + '【文件已存在】')39     except Exception as e:40         print("爬取失败" + str(e))41 42 43 # 获取imgHtml标签44 def getcontent(soup):45     for i in soup.find_all('img', class_='pic-large'):46         imgsrc = i['src']47         if imgsrc.find('http') >= 0 or imgsrc.find('https') >= 0:48             # 下载图片49             downImg(imgsrc)50 51 52 # 根据url获取html源码53 def getHtmlByUrl(htmlUrl):54     htmlText = requests.get(htmlUrl).content55     # 使用beautifulSoup解析html56     soup = BeautifulSoup(htmlText, 'lxml')57 58     return soup59 60 61 def main():62     htmlUrlList = getUrlList()63     for url in htmlUrlList:64         htmltext = getHtmlByUrl(url)65         getcontent(htmltext)66 67 68 if __name__ == '__main__':69     main()

三、结果

四、总结

  代码用比较笨的方法来获取,先试水

转载于:https://www.cnblogs.com/milicool/p/11262684.html

你可能感兴趣的文章
你应该首先保护哪些应用程序?这个问题本身问错了!
查看>>
C++之检测文件结尾
查看>>
【Delphi】注册快捷键
查看>>
字符串、文件操作,英文词频统计预处理
查看>>
[TJOI2014]Alice and Bob[拓扑排序+贪心]
查看>>
[SDOI2017]天才黑客[最短路、前缀优化建图]
查看>>
Asp.Net中的三种分页方式
查看>>
在线文件格式转换
查看>>
如何在eclipse中使用XYLayout布局?在此介绍如何把XYLayout导入到eclipse .
查看>>
sql之left join、right join、inner join的区别(转)
查看>>
C#调用ArcGIS REST服务
查看>>
JDBC 使用详解
查看>>
【TensorFlow篇】--DNN初始和应用
查看>>
潇洒鸿图
查看>>
SerializableMaplist传递数据
查看>>
javascript_json创建对象
查看>>
实验吧CTF题库 Forbidden 利用burpsuite进行抓包改包
查看>>
深度优先算法与广度优先算法
查看>>
提交图片
查看>>
走进AngularJs(一)angular基本概念的认识与实战
查看>>