首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Python爬虫视频以及使用python3爬取的实例

链接: https://pan.baidu.com/s/18iRD2I9t4xHxiSqoe-hFHg 密码: afaf

使用Python3爬取小说,代码看起来有点乱,下面有截图

import requests

from lxml import etree

from multiprocessing.dummy import Pool

import os

import re

#小说的章节地址

chapter_url = "https://www.biqudu.com/43_43821/"

#忽略警告 https请求设置verify=False时 有时候会报错 设置这条语句可以解决

def get_response(url):

'''

:根据指定URL获取响应数据、

:返回xpath选择器格式的数据

'''

html = requests.get(url,verify=False)

return etree.HTML(html.text)

def get_chapter_content(selector):

'''

:传入xpath选择器格式的数据获取想要的数据

:返回一个包含章节标题,和章节地址的数组

'''

html = []

#根据xpath获取title

title = selector.xpath('//*[@id="list"]/dl/dd/a/text()')

# 根据xpath获取url

href = selector.xpath('//*[@id="list"]/dl/dd/a/@href')

#这里以12开始遍历是因为前面几个数据是不要的

for i in range(12,len(title)):

tit = title[i]

url = "https://www.biqudu.com" + href[i]

chapter = {"title":tit,"url":url}

html.append(chapter)

return html

def save_content(url):

'''

:根据传进来的URL获取数据并保存

:这里的URL传入的事一个字典存储的地址和相对应保存文件的名称

'''

# 保存下载文件的文件夹

folder = 'novel'

# 获取选择器

html = get_response(url['url'])

# 提取出想要的内容

con = html.xpath('//*[@id="content"]/text()')

# 判断文件夹是否存在 不存在就创建

os.mkdir(folder)

# 去掉非法字符

fileName = re.sub('[\/:*?"|]','-',url['name'])

# 保存文件

with open(folder + "/" + fileName + ".txt","w+",encoding="utf-8") as f:

# 得到的是一个list 这里转换为str

content = ''.join(con)

#遍历字符串 保存为每行不好过50个字符

for i in range(0,len(content),50):

f.write(content[i:i+50] + "\n")

def get_content(html):

'''

:并行爬取保存数据

'''

urls = []

for con in html:

url = con['url']

name = con['title']

urls.append({'name':name,'url':url})

# 线程个数

pool = Pool(4)

# 使用map进行并行爬取,save_content为爬取保存函数,

# urls为一个list,里面存储的为网址列表和对应的保存名字

pool.map(save_content,urls)

pool.close()

pool.join()

def main():

selector = get_response(chapter_url)

html = get_chapter_content(selector)

get_content(html)

if __name__ == '__main__':

main()

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20180607G13S1800?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券