string = “www.shenhuxi.com.cn”
print(string.split(‘.’)) ==>[”www’,’shenhuxi’,’com’,’cn’]
2.分割两次
print(string.split(‘.’,2)) ===>[‘www’,’shenhuxi’,’com.cn’]
response是requests的响应
response.url是返回爬取的页面的链接,
filename = response.url.split(“/”)[-2] 分离字符串
3.分割两次
print(string.split(‘.’,2)[1]) =====>分割两次,并且取序列为1的项=====>shenhuxi
二 分离文件名和路径
import os
print(os.path.split(‘/dodo/soft/python/’))
(‘/dodo/soft/python/’,”)
print(os.path.split(‘/dodo/soft/python’))
(‘/dodo/soft’,’python’)
li = ”.join(list2)
print(li)
mingtianhuigenghao
ming:tian:hui:geng:hao:
import os
lis = os.path.join(‘/ming/’,’tian/hui/’,’genghao/’)
‘/mingtianhuigenghao’