鱼C论坛

 找回密码
 立即注册
查看: 3985|回复: 7

[已解决]如何提取指定内容?

[复制链接]
发表于 2018-2-7 16:14:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 checkily 于 2018-2-7 16:19 编辑
  1. str1 = '''<a  target="http://www.fishc.com/dvd/1"target="_blank">鱼C资源打包1</a>
  2.              <a  target="http://www.fishc.com/dvd/2"target="_blank">鱼C资源打包2</a>
  3.              <a  target="http://www.fishc.com/dvd/3"target="_blank">鱼C资源打包3</a>
  4.              <a  target="http://www.fishc.com/dvd/4"target="_blank">鱼C资源打包4</a>
  5.              <a  target="http://www.fishc.com/dvd/5"target="_blank">鱼C资源打包5</a>'''
复制代码


如何把网址提取出来?即是把“http://www.fishc.com/dvd/1”、“http://www.fishc.com/dvd/2”........等提取出来?
最佳答案
2018-2-8 12:52:26
  1. str1 = '''<a  target="http://www.fishc.com/dvd/1"target="_blank">鱼C资源打包1</a>
  2.              <a  target="http://www.fishc.com/dvd/2"target="_blank">鱼C资源打包2</a>
  3.              <a  target="http://www.fishc.com/dvd/3"target="_blank">鱼C资源打包3</a>
  4.              <a  target="http://www.fishc.com/dvd/4"target="_blank">鱼C资源打包4</a>
  5.              <a  target="http://www.fishc.com/dvd/5"target="_blank">鱼C资源打包5</a>'''
  6. i=0
  7. strq=len("<a  target=")
  8. strh=len('target="_blank">鱼C资源打包1</a>')
  9. #建个空列表
  10. yihan=['']
  11. #分割多重字符串  分成一行加入到列表
  12. for each in str1:
  13.     yihan[i]+=each
  14.     if each=='\n':
  15.         #去掉前后空格
  16.         yihan[i]=yihan[i].strip()
  17.         #字符串分片   取出想要的地方
  18.         yihan[i]= yihan[i][strq:-strh]
  19.         print(yihan[i])
  20.         i+=1
  21.         #列表尾部加入成员
  22.         yihan.append('')
  23.         continue
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-2-8 12:52:26 | 显示全部楼层    本楼为最佳答案   
  1. str1 = '''<a  target="http://www.fishc.com/dvd/1"target="_blank">鱼C资源打包1</a>
  2.              <a  target="http://www.fishc.com/dvd/2"target="_blank">鱼C资源打包2</a>
  3.              <a  target="http://www.fishc.com/dvd/3"target="_blank">鱼C资源打包3</a>
  4.              <a  target="http://www.fishc.com/dvd/4"target="_blank">鱼C资源打包4</a>
  5.              <a  target="http://www.fishc.com/dvd/5"target="_blank">鱼C资源打包5</a>'''
  6. i=0
  7. strq=len("<a  target=")
  8. strh=len('target="_blank">鱼C资源打包1</a>')
  9. #建个空列表
  10. yihan=['']
  11. #分割多重字符串  分成一行加入到列表
  12. for each in str1:
  13.     yihan[i]+=each
  14.     if each=='\n':
  15.         #去掉前后空格
  16.         yihan[i]=yihan[i].strip()
  17.         #字符串分片   取出想要的地方
  18.         yihan[i]= yihan[i][strq:-strh]
  19.         print(yihan[i])
  20.         i+=1
  21.         #列表尾部加入成员
  22.         yihan.append('')
  23.         continue
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-2-8 15:28:27 | 显示全部楼层
第22行,为什么要在尾部加入空的成员?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-8 17:54:34 | 显示全部楼层
checkily 发表于 2018-2-8 15:28
第22行,为什么要在尾部加入空的成员?

那是一个列表,以开始只有一个成员,如果不加,yihan[1]就出错了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-8 18:02:25 | 显示全部楼层
  1. str1 = '''<a  target="http://www.fishc.com/dvd/1"target="_blank">鱼C资源打包1</a>
  2.              <a  target="http://www.fishc.com/dvd/2"target="_blank">鱼C资源打包2</a>
  3.              <a  target="http://www.fishc.com/dvd/3"target="_blank">鱼C资源打包3</a>
  4.              <a  target="http://www.fishc.com/dvd/4"target="_blank">鱼C资源打包4</a>
  5.              <a  target="http://www.fishc.com/dvd/5"target="_blank">鱼C资源打包5</a>'''

  6. strq=len("<a  target=")
  7. strh=len('target="_blank">鱼C资源打包1</a>')
  8. # splitlines() 按照’\n’分割,返回一个包含各行作为元素的列表
  9. #splitlines() 字符串的内置方法
  10. yihan=str1.splitlines()
  11. for i in range(0,len(yihan)):
  12.     #去掉前后空格
  13.     yihan[i]=yihan[i].strip()
  14.     #字符串分片   取出想要的地方
  15.     yihan[i]= yihan[i][strq:-strh]
  16.     print(yihan[i])
复制代码

用个字符串BIF更简洁
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-2-8 19:39:01 | 显示全部楼层
waitforlove 发表于 2018-2-8 18:02
用个字符串BIF更简洁


我对strq和strh明白。就是取出前面没用的字符串长度,和后面没用的字符串长度,然后切片截出中间部分
但如果这个str1每行后面的长度不固定呢?又怎样?
现在都是"鱼C资源包1</a>",如果后面多了,变成“鱼C进阶资源包100”、“鱼C超级VIP资源包1000”
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-8 21:03:49 From FishC Mobile | 显示全部楼层
调用字符串的bif。 yihan. find (''<a  target=")返回找到的位置
yihan.find(target) 前后位置都知道了切片,这我想的思路
手机就不打代码了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-8 21:09:09 From FishC Mobile | 显示全部楼层
再复杂我也不会了,老鱼的视频才看了16节,我新人
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-20 11:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表