-1

尝试将 URL 与正则表达式匹配时,我得到以下 URL。我该如何解决这个错误。我试图使用正则表达式从 href 中提取文本并将其附加到原始 URL 。类似于重定向的东西。由于未经授权,我无法发布 URL。

示例 URL 是 abc.com

我正在解析上面的 URL 以从下拉菜单中提取 href 文本,假设文本是<li><a href="ABC.asp?DER=PI">Myshop</a></li> 我从 a href 中提取数据并将 URL 设为 abc.com/ABC.asp?DER=PI

尝试将 URL 与正则表达式匹配并与 URL 字符串连接时,我得到以下 URL。我该如何解决这个错误

a=(re.compile('href=(.+Home\.asp\?Pipe=.+)\"'))

我收到以下错误。

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-189-01866482c481> in <module>
     10 for item in data1:
     11     a=(re.compile('href=(.+Home\.asp\?Pipe=.+)\"'))
---> 12     print(a + url)
     13 #print(data2)
     14 #for item in data

TypeError: unsupported operand type(s) for +: '_regex.Pattern' and 'str'
4

3 回答 3

1

re.compile将正则表达式模式编译为正则表达式对象,该对象只能使用其 match() 和 search() 方法进行匹配。

您不能使用字符串添加正则表达式对象。

请阅读https://docs.python.org/3.7/library/re.html了解更多信息。

我希望它有帮助

于 2019-06-20T20:06:49.097 回答
0

如果您只想提取href

import re
txt = '<li><a href="ABC.asp?DER=PI">Myshop</a></li>'
url = 'abc.com'
find_href = re.compile(r'href="(.+)"')
href = find_href.search(txt)
if href:
    print(f"{url}/{href.group(1)}")

输出:

abc.com/ABC.asp?DER=PI
于 2019-06-20T21:59:56.783 回答
0

@akshay re.compile(pattern) => 返回 re 对象,您正在尝试使用字符串执行连接操作(即 url - 由于未提供完整的代码片段而不清楚),这是不允许的。请提供完整的代码片段和它的用途,以便我可以更好地了解它。谢谢

于 2019-06-21T03:44:33.310 回答