如何提取指定内容?
本帖最后由 checkily 于 2018-2-7 16:19 编辑str1 = '''<atarget="http://www.fishc.com/dvd/1"target="_blank">鱼C资源打包1</a>
<atarget="http://www.fishc.com/dvd/2"target="_blank">鱼C资源打包2</a>
<atarget="http://www.fishc.com/dvd/3"target="_blank">鱼C资源打包3</a>
<atarget="http://www.fishc.com/dvd/4"target="_blank">鱼C资源打包4</a>
<atarget="http://www.fishc.com/dvd/5"target="_blank">鱼C资源打包5</a>'''
如何把网址提取出来?即是把“http://www.fishc.com/dvd/1”、“http://www.fishc.com/dvd/2”........等提取出来? str1 = '''<atarget="http://www.fishc.com/dvd/1"target="_blank">鱼C资源打包1</a>
<atarget="http://www.fishc.com/dvd/2"target="_blank">鱼C资源打包2</a>
<atarget="http://www.fishc.com/dvd/3"target="_blank">鱼C资源打包3</a>
<atarget="http://www.fishc.com/dvd/4"target="_blank">鱼C资源打包4</a>
<atarget="http://www.fishc.com/dvd/5"target="_blank">鱼C资源打包5</a>'''
i=0
strq=len("<atarget=")
strh=len('target="_blank">鱼C资源打包1</a>')
#建个空列表
yihan=['']
#分割多重字符串分成一行加入到列表
for each in str1:
yihan+=each
if each=='\n':
#去掉前后空格
yihan=yihan.strip()
#字符串分片 取出想要的地方
yihan= yihan
print(yihan)
i+=1
#列表尾部加入成员
yihan.append('')
continue 第22行,为什么要在尾部加入空的成员? checkily 发表于 2018-2-8 15:28
第22行,为什么要在尾部加入空的成员?
那是一个列表,以开始只有一个成员,如果不加,yihan就出错了
str1 = '''<atarget="http://www.fishc.com/dvd/1"target="_blank">鱼C资源打包1</a>
<atarget="http://www.fishc.com/dvd/2"target="_blank">鱼C资源打包2</a>
<atarget="http://www.fishc.com/dvd/3"target="_blank">鱼C资源打包3</a>
<atarget="http://www.fishc.com/dvd/4"target="_blank">鱼C资源打包4</a>
<atarget="http://www.fishc.com/dvd/5"target="_blank">鱼C资源打包5</a>'''
strq=len("<atarget=")
strh=len('target="_blank">鱼C资源打包1</a>')
# splitlines() 按照’\n’分割,返回一个包含各行作为元素的列表
#splitlines() 字符串的内置方法
yihan=str1.splitlines()
for i in range(0,len(yihan)):
#去掉前后空格
yihan=yihan.strip()
#字符串分片 取出想要的地方
yihan= yihan
print(yihan)
用个字符串BIF更简洁 waitforlove 发表于 2018-2-8 18:02
用个字符串BIF更简洁
我对strq和strh明白。就是取出前面没用的字符串长度,和后面没用的字符串长度,然后切片截出中间部分
但如果这个str1每行后面的长度不固定呢?又怎样?
现在都是"鱼C资源包1</a>",如果后面多了,变成“鱼C进阶资源包100”、“鱼C超级VIP资源包1000”
调用字符串的bif。 yihan. find (''<atarget=")返回找到的位置
yihan.find(target) 前后位置都知道了切片,这我想的思路
手机就不打代码了 再复杂我也不会了,老鱼的视频才看了16节,我新人
页:
[1]