Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中的一个re.compile语句中排除/包括字符串以提取感兴趣的URL_Python_Regex_Beautifulsoup - Fatal编程技术网

在python中的一个re.compile语句中排除/包括字符串以提取感兴趣的URL

在python中的一个re.compile语句中排除/包括字符串以提取感兴趣的URL,python,regex,beautifulsoup,Python,Regex,Beautifulsoup,因此,我尝试获取包含特定字符串的URL,但也避免了包含坏字符串的URL 因此,我不希望任何URL包含字符串“/inventory/all/”,我只希望URL包含字符串“/inventory/”或“/inventory/2017/” 因此,我至少通过以下方式排除了包含“/inventory/all/”字符串的URL: 但是,当我尝试包含我确实希望获得的字符串时,它不再有效,我尝试: get_urls = soup.findAll('a', href=re.compile('^(?!.*/inven

因此,我尝试获取包含特定字符串的URL,但也避免了包含坏字符串的URL

因此,我不希望任何URL包含字符串“/inventory/all/”,我只希望URL包含字符串“/inventory/”或“/inventory/2017/”

因此,我至少通过以下方式排除了包含“/inventory/all/”字符串的URL:

但是,当我尝试包含我确实希望获得的字符串时,它不再有效,我尝试:

get_urls = soup.findAll('a', href=re.compile('^(?!.*/inventory/all/).*$'|/inventories/|/inventory/2017/'))

谢谢你的帮助,我是个新手

你可以使用以下正则表达式:

^(?=.*inventor(?:ies|y/2017))^(?:(?!inventory/all).)+$
  • ^(?=.*inventor(?:ies | y/2017))
    这是一种前瞻性的方法,可以确保我们只查找具有
    库存
    库存/2017
    的字符串。为了减少回溯,您需要锚定它,即显示匹配应该从句子的开头开始。因此,只要做
    ^.*inventor(?:ies | y/2017)。*$
    就足够了,因为只选择了两个

  • ^((?!inventory/all)。+$
    这部分是一个负面展望,它断言从字符串开始到字符串结束没有
    inverntory/all
    。我添加了此部分,以防您发现格式为
    inventoy/2017/inventory/all
    的字符串将被删除


汤对象包含什么?您是否有剪切粘贴错误,或者在第一个垂直条前面有一个额外的单引号是语法错误:
*$'|/inv
就在那里。是的,我最终通过href=re.compile('^(?!*/inventory/all/).*/inventory/*$)解决了这个问题。。。谢谢你的建议
^(?=.*inventor(?:ies|y/2017))^(?:(?!inventory/all).)+$