正则表达式不按要求工作

时间:2016-07-27 09:45:38

标签: python html regex

这是我的HTML代码:

<ul class="hide menuSearchType">
    <li><a href="../../dynamic/city_select.aspx">Search by city</a></li>
    <li><a href="../../searchbyphone.aspx">Search by phone</a></li>
    <li><a href="../searchbyaddress.aspx">Search by address</a></li>
    <li><a href="../searchbybrand.aspx">Search by brand</a></li>
    <li><a href="/advertisement-center/">Advertise with us</a></li>
    <li><a href="/advertisement-center/">Advertise with us</a></li>
    <li><a href="//fonts.googleapis.com/css?family=Open+Sans">Find a Person</a></li>
    <li><a href="//fonts.googleapis.com/css?family=Open+Sans">Find a Person</a></li>
    <li><a href="dynamic/city_select.aspx">Search by city</a></li>
    <li><a href="searchbybrand.aspx">Search by brand</a></li>
</ul>

这是我的Python代码:

import re, os
from urllib.parse import urlparse

url = "http://www.phonebook.com.pk/dynamic/search.aspx?searchtype=cat&class_id=2566" 

path = urlparse(url)
lpath = os.path.dirname(path.path)

html = u"<ul class=\"hide menuSearchType\">\n    <li><a href=\"../../dynamic/city_select.aspx\">Search by city</a></li>\n    <li><a href=\"../../searchbyphone.aspx\">Search by phone</a></li>\n    <li><a href=\"../searchbyaddress.aspx\">Search by address</a></li>\n    <li><a href=\"../searchbybrand.aspx\">Search by brand</a></li>\n    <li><a href=\"/advertisement-center/\">Advertise with us</a></li>\n    <li><a href=\"/advertisement-center/\">Advertise with us</a></li>\n    <li><a href=\"//fonts.googleapis.com/css?family=Open+Sans\">Find a Person</a></li>\n    <li><a href=\"//fonts.googleapis.com/css?family=Open+Sans\">Find a Person</a></li>\n    <li><a href=\"dynamic/city_select.aspx\">Search by city</a></li>\n    <li><a href=\"searchbybrand.aspx\">Search by brand</a></li>\n</ul>"

linkList1 = re.findall(re.compile(u'(?<=href=")../.*?(?=")'), str(html))

for link1 in linkList:
    html = re.sub(link1, path.scheme + "://" + os.path.normpath(path.netloc + os.path.abspath(lpath + "/" + link1)), str(html))

print (html)

问题是它是否按预期检测到带有“../”的链接,但也改变了“../../”,有什么方法可以限制我的正则表达式只用单个“../选择网址“?

预期产出:

<ul class="hide menuSearchType">
    <li><a href="../../dynamic/city_select.aspx">Search by city</a></li>
    <li><a href="../../searchbyphone.aspx">Search by phone</a></li>
    <li><a href="http://www.phonebook.com.pk/searchbyaddress.aspx">Search by address</a></li>
    <li><a href="http://www.phonebook.com.pk/searchbybrand.aspx">Search by brand</a></li>
    <li><a href="/advertisement-center/">Advertise with us</a></li>
    <li><a href="/advertisement-center/">Advertise with us</a></li>
    <li><a href="//fonts.googleapis.com/css?family=Open+Sans">Find a Person</a></li>
    <li><a href="//fonts.googleapis.com/css?family=Open+Sans">Find a Person</a></li>
    <li><a href="dynamic/city_select.aspx">Search by city</a></li>
    <li><a href="searchbybrand.aspx">Search by brand</a></li>
</ul>

3 个答案:

答案 0 :(得分:2)

您可以使用正则表达式替换字符串,

output = re.sub(r'(?is)(href="../)([^.])','http://www.phonebook.com.pk/'+r'\2',str(html))

答案 1 :(得分:2)

按要求使用BeautifulSoup:

from bs4 import Beautifulsoup
soup = BeautifulSoup(html)
all = soup.select('li')
for i in all:
    try:
        output = re.sub(r'(?is)(href="../)([^.])','http://www.phonebook.com.pk/'+r'\2',str(i))
    except:
        output = i
    print(output)

答案 2 :(得分:1)

尝试使用以下内容:

linkList1 = re.findall(re.compile(u'(?<=href=")../\w.*?(?=")'), str(html))

这保证了斜线后必须有一个单词字符。