Python取代不替换所有东西

时间:2018-01-22 23:36:29

标签: python replace

我试图在我的.html文件中将所有链接替换为开头的斜杠./。例如:

<link href="/wp-content/themes/newera/style.css?ver=1" id="stylesheet-css" media="all" rel="stylesheet" type="text/css"/>

<link href="../wp-content/themes/newera/style.css?ver=1" id="stylesheet-css" media="all" rel="stylesheet" type="text/css"/>

所以我得到了这段代码:

with open(path, 'r') as file :
     filedata = str(file.read())
     filedata = filedata.replace('href="/', 'href="../')
     filedata = filedata.replace('src="/', 'src="../')
 with open(path, 'w') as file:
     file.write(filedata)

它取代但不是一切。在.html文件中运行函数之后是这个(代码的一部分):

<link href="../feed/" rel="alternate" title="avium » Feed" type="application/rss+xml"/>
  <link href="../comments/feed/" rel="alternate" title="avium » Comments Feed" type="application/rss+xml"/>
  <link href="../about/feed/" rel="alternate" title="avium » About Comments Feed" type="application/rss+xml"/>
  <link href="../wp-content/uploads/2014/11/favicon.ico" rel="shortcut icon"/>
  <link href="../wp-content/plugins/fuse-social-floating-sidebar/inc/css/font-awesome.min.css" rel="stylesheet"/>
  <link href="/wp-content/themes/newera/style.css?ver=1" id="stylesheet-css" media="all" rel="stylesheet" type="text/css"/>
  <link href="/wp-content/themes/newera/framework/css/jquery.bxslider.css?ver=1" id="oi_bxslider_css-css" media="all" rel="stylesheet" type="text/css"/>
  <link href="/wp-content/themes/newera/framework/FlexSlider/flexslider.css?ver=1" id="flex-slider-css" media="all" rel="stylesheet" type="text/css"/>
  <link href="/wp-content/themes/newera/framework/css/prettyPhoto.css?ver=1" id="oi_prettyPhoto_css-css" media="all" rel="stylesheet" type="text/css"/>
  <link href="/wp-content/plugins/contact-form-7/includes/css/styles.css?ver=4.0.1" id="contact-form-7-css" media="all" rel="stylesheet" type="text/css"/>
  <link href="/wp-content/plugins/revslider/rs-plugin/css/settings.css?rev=4.6.0&ver=4.0.18" id="rs-plugin-settings-css" media="all" rel="stylesheet" type="text/css"/>

为什么它没有取代所有东西?

编辑:

好了这里的完整代码:https://pastebin.com/n25f0CTh

我的全部职能是:

with open(path, 'r') as file :
     filedata = str(file.read())
     filedata = filedata.replace('http://'+domain, '')
     filedata = filedata.replace('http://www.'+domain, '')
     filedata = filedata.replace('www.'+domain, '')
     filedata = filedata.replace(domain, '')
     filedata = filedata.replace('href="/', 'href="../')
     filedata = filedata.replace('src="/', 'src="../')
     filedata = filedata.replace('href=""', 'href="../index.html"')
 with open(path, 'w') as file:
     file.write(filedata)

变量&#39;域&#39;在这个例子中:avium.pl

1 个答案:

答案 0 :(得分:0)

单引号和双引号之间似乎存在不一致,例如

<link rel="shortcut icon" href="http://avium.pl/wp-content/uploads/2014/11/favicon.ico">

<link rel='stylesheet' id='oi_bxslider_css-css'  href='http://avium.pl/wp-content/themes/newera/framework/css/jquery.bxslider.css?ver=1' type='text/css' media='all' />

添加

filedata = filedata.replace("href='/", "href='../")
filedata = filedata.replace("src='/", "src='../") 

似乎抓住了一切