Python 爬虫

import requests
#正则模块
import re


# 要爬的网站
url = 'http://'


# 模拟浏览器发送http请求
response = requests.get(url)
# 编码方式
response.encoding = 'utf-8'
# 目标小说主页的网页源码
html = response.text
# 小说名
title = re.findall(r'<meta property="og:novel:book_name" content="(.*?)" />',html,re.S)[0]
# 新建一个文件,以小说名命名
fb = open('%s.txt' % title, 'w', encoding='utf-8')
# 获取每一章的信息(章节,url)
# re.S .匹配任意字符包括不可见字符(空格回车)
chapter_info_list = re.findall(r'<a rel="nofollow" href="(.*?)">(.*?)</a>', html, re.S) # 第二个参数为string,使用str()将列表转换为string
#循环每一个章节,分别下载
for chapter_info in chapter_info_list:
    chapter_title = chapter_info[1]
    chapter_url = chapter_info[0]
    chapter_info = chapter_info_list[0]
    chapter_url, chapter_title = chapter_info # 与注释掉的两句同义
    chapter_response = requests.get('http://www.xqiushu.com/t/17591/5788711.html')
    chapter_response.encoding = 'utf-8'
    chapter_html = chapter_response.text
    chapter_content = re.findall(r'<div class="book_content" id="content">(.*?)<div class="con_l">', html, re.S)
    fb.write(chapter_title)
    fb.write(str(chapter_content))#使用str()将列表转换为string
    fb.write("\n")

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值