Python爬取小说《完美世界》
由于是新手,Python刚入门不久,写的不好之处,还请各大神谅解。
这里参考了大神的博客:Python每日一练(18)-抓取小说目录和全文
首先打开需要爬取得小说地址:完美世界
打开网址后如下图:
通过分析网页代码,如上图所示:发现所有章节在div id="list"的下面,代码如下。
def get_info(url):
response = requests.get(url,headers=headers)
response.encoding = 'utf-8'
get_info_list = []
html = etree.HTML(response.text)
dd_list = html.xpath('//*[@id="list"]/dl/dd')
for dd in dd_list:
title = dd.xpath('a/text()')[0]
href = 'http://www.biquku.la/0/35/' + dd.xpath('a/@href')[0]
chapter = {
'title':title,'href':href}
get_info_list