[python爬虫]--爬取豆瓣音乐topX
来源:互联网 发布:国内英文域名 编辑:程序博客网 时间:2024/05/14 20:06
最近在学习python爬虫,写出来的一些爬虫记录在csdn博客里,同时备份一个放在了github上。
github地址:https://github.com/wjsaya/python_spider_learn/
本次内容:从煎蛋网下载图片
思路:
- 拼接出所需的页面URL。
- 用BS去访问和解析页面URL,获取页面内的所需图片并保存到本地并重命名。
代码:
#coding: utf-8import timeimport reimport osimport requestsfrom bs4 import BeautifulSoupurl = 'http://jandan.net/ooxx'firefox = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0 FirePHP/0.7.4"}Pg_count = input("请输入想要获取的页数(直接回车默认页码为1):")if Pg_count == "": Pg_count = "1"Pg_count=int(Pg_count)-1#给get_html函数使用,预先+1上去,这样-1就是当前第一页。def get_content(url):#获取url,返回BS解析之后的content html = requests.get(url, headers=firefox) content = BeautifulSoup(html.content, "lxml") return contentdef get_current_page(content):#获取url,返回url中的current-comment-page(即页码) page_swap1 = content.find('span',class_="current-comment-page") page_swap2 = str(page_swap1) current_page = page_swap2[36:39] #current_page = str(content.find('span',class_="current-comment-page"))[36:39] #此处本来是写成了一句语句直接提取,后来想了想,万一以后想再看下,这会把自己绕晕,于是乎拆分了、、、 return current_pagedef get_img_url(content, max_page):#获取解码后的html文件以及最大页码。获取html中的图片img_url,然后把img_url和最大页码传递给get_img函数。 #html = BeautifulSoup(content, "lxml") img_li = content.find_all("a", class_="view_img_link") for i in img_li: img_url = "http:"+i["href"] #img_url = "http://"+i["href"].strip("/") get_img(img_url, max_page) #strip,用来删除某字符 #并且加上http协议头def get_img(img_url, floder):#获取img_url和文件夹名(即当前网页页码),通过img_url下载图片并保存在floder内。 img_name = img_url[28:] img_file = requests.get(img_url, headers=firefox) q = img_file.content with open ("./get/"+floder+"/"+img_name, 'wb') as img_file: img_file.write(q) print (floder+"/"+img_name+" is downloaded")def get_html(max_page, Pg_count):#获取最大页码数及想要下载图片的页数,循环得出每页的url地址。 #http://jandan.net/ooxx/page-107#comments while Pg_count >= 0: pg = int(max_page)-Pg_count url = "http://jandan.net/ooxx/page-"+str(pg)+"#comments" print ("当前获取到的包含图片的页面url地址为:"+url) Pg_count -= 1 content = get_content(url) print ("开始创建图片存放目录并获取图片链接地址......") if os.path.exists("./get/") == False: os.mkdir ("./get/") if os.path.exists("./get/"+str(pg)) == False: os.mkdir ("./get/"+str(pg)) print ("开始下载图片,本地存放目录为:./get/"+str(pg)) time.sleep(3) get_img_url(content, str(pg))def main():#主函数,一切的开端 content = get_content(url) current_page = get_current_page(content) get_html(current_page, int(Pg_count))if __name__ == '__main__': main()
下载过程截图:
恩。。。效果图:
阅读全文
0 0
- [python爬虫]--爬取豆瓣音乐topX
- [python爬虫]--爬取豆瓣音乐topX
- 利用python爬取豆瓣音乐TOP250
- Python爬虫 爬取豆瓣读书
- Python爬虫-爬取豆瓣读书
- <Python爬虫>爬取豆瓣图书/豆瓣电影系列
- python爬虫之爬取百度音乐
- python爬虫之爬取百度音乐
- python定时器爬取豆瓣音乐Top榜歌名
- Python—爬虫—简单爬取豆瓣电影
- 用Python爬虫爬取豆瓣TOP250电影
- Python 采用Scrapy爬虫框架爬取豆瓣电影top250
- [python爬虫入门]爬取豆瓣电影排行榜top250
- python爬虫实战 | 爬取豆瓣TOP250排名信息
- Python爬虫实战:Scrapy豆瓣电影爬取
- [Python/爬虫]利用xpath爬取豆瓣电影top250
- python爬虫爬取豆瓣书籍信息并生成表格
- Python爬虫实战(一):爬取豆瓣电影top250排名
- 【三】java从头开始学习之赶紧搬家吧,CSDN太坑了
- 写给大数据开发初学者的话
- 什么是H.265编解码器和H.265优于H.264的优势?
- Python中文乱码问题原理解释
- JAVA JDK1.5-1.9新特性
- [python爬虫]--爬取豆瓣音乐topX
- MySQL 5.5 Command Line Client 打开出现闪退(错误)的解决办法
- ShaderLab和CG/HLSL的一些数据类型归纳
- 字符串转ASC
- 精卫之家
- AngularJS——ui-router详解
- poj 2750 Potted Flower(线段树)
- Android WebView 参数设置明细!
- Git之集中式vs分布式