python爬虫

批量下载懒人听书mp3文件

懒人听书是一个不错的在线听书网站, 最近用电脑网页在听。打算把音频文件给下载到手机上,平时的空余时间听听。不过网页版并不提供下载功能。

只有安装app后才能下载。

装了app也只能一个一个地去下载,所以索性一次过下载全部的音频吧。

方法很简单,代码很短。 你也可以看得懂。

以财经郎眼的节目音频为例。 其他的节目内容方法差不多的。

浏览器打开页面:

http://www.lrts.me/book/32551

然后按F12打开调试窗口,我用的是chrome

然后看到网页底下有下一页的按钮,点击下一页,看看每一页的url. 就能找到具体的下一页的url.

(待续)

python代码:# coding: utf-8

# http://30daydo.com

import urllib

import os

import requests

import time

from lxml import etree

from header_toolkit import getheader

def spider():

curr=os.getcwd()

target_dir=os.path.join(curr,'data')

if not os.path.exists(target_dir):

os.mkdir(target_dir)

for i in range(1, 100, 10):

url = 'http://www.lrts.me/ajax/playlist/2/32551/%d' % i

headers = {

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'}

s = requests.get(url=url, headers=headers)

tree = etree.HTML(s.text)

nodes = tree.xpath('//*[starts-with(@class,"clearfix section-item section")]')

print len(nodes)

for node in nodes:

filename = node.xpath('.//div[@class="column1 nowrap"]/span/text()')[0]

link = node.xpath('.//input[@name="source" and @type="hidden"]/@value')[0]

print link

post_fix=link.split('.')[-1]

full_path= filename+'.'+post_fix

urllib.urlretrieve(link, filename=os.path.join(target_dir,full_path))

time.sleep(1)

if __name__ == '__main__':

spider()

抓取的内容:

原创文章,转载请注明出处:

http://30daydo.com/article/231

需要下载的打包数据可以留下邮箱或者私信。

0

分享

微博

QZONE

微信

2017-09-14

7 个评论

ppluda

是半拉子文章吗

2017-09-21 16:31

李魔佛 回复 ppluda

完整的文章,你可以自己运行试试。

2018-03-11 13:58

python3259

感谢共享

2018-06-03 15:57

nangongmujd

您好! 其中'http://www.lrts.me/ajax/playlist/2/'怎么获取的呀?

2018-10-30 16:38

李魔佛 回复 nangongmujd

通过抓包。

2018-10-30 18:13

wanqiushiyu

怎么抓包

2019-03-12 15:32

李魔佛 回复 wanqiushiyu

这个慢慢调一下就可以了

2019-03-13 22:12

要回复文章请先登录或注册