python用字符串操作20行代码简单爬虫入门+案例(爬取一章《三体》小说)

clipboard.png

所需要的简单的方法

1、#导入专用包

import urllib.request

2、try...except..

try:
   语句1....
except Exception as e:
    语句2...
尝试执行语句1,执行不成功就执行语句2

3、urlopen获取内容

response =urllib.request.urlopen(webList)
#获取webList页面的内容

4、read()读取

response.read()
#读取获取的内容

5、decode解码

decode('UTF-8')
#用utf-8的方式解码

6、替换方法

html = html.expandtabs()
#html内容替换所有的制表符为空

html =html.replace(' ','')
#替换掉所有的空格

7、获取长度

lenth = len(html)
#获取文档的长度

8、find()查找方法

lenth = len(html)
#获取文档的长度

9、字符串的截取

html[0:index2]
#对整篇字符串进行截取

10、写入 open..write

writeFile =open('三体.txt','w')
writeFile.write(htm)
#写入文件

案例爬取一页《三体》小说。

#导入专用包
import urllib.request
#需要连接的页面
webList ='http://www.51shucheng.net/kehuan/santi/santi1/174.html'
#用try尝试去连接
try:
    response =urllib.request.urlopen(webList)
    #如果能成功连接,并获取内容,response就是我们所获取的那个页面
except Exception as e:
    print('获取失败')
    #否则就打印出‘获取失败’
html = str(response.read().decode('UTF-8'))
# 把获取的内容读取出来,并且用UTF-8解码
html = html.expandtabs()
#替换掉所有的TAB符号
html =html.replace(' ','')
#替换掉所有的空格
print(html)
#可以打印出来预览一下,方便进行定位
lenth = len(html)
#获取文档的长度
html = html[html.find('neirong">',0,lenth)+9:]
index =html.find('跟鞋。</p>',0)+3
index2 = html.find('眷恋着天空。</p>')
index3 =html.find('<p>“红色联合”的战士们欢呼起来')
#找到一些关键位置,获取索引,方便下边进行定位
htm =str(html[0:index2]+html[index3:index])
#对整篇字符串进行截取
htm = htm.replace('<p>','    ')
htm = htm.replace('</p>','\n')
#把文中的<p></p>替换掉
writeFile =open('三体.txt','w')
writeFile.write(htm)
#写入文件
print('写入完成')

clipboard.png

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值