Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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 使用正则表达式改进html代码中的过滤器链接_Python_Regex - Fatal编程技术网

Python 使用正则表达式改进html代码中的过滤器链接

Python 使用正则表达式改进html代码中的过滤器链接,python,regex,Python,Regex,我的项目的目的是webscrapa搜索引擎(我选择了DuckDuckGo)。要获取第一页上的所有链接,然后输入其中的每个链接,请获取HTML源代码并执行正则表达式,该表达式将过滤HTML代码中的所有.onion网站 从这里开始,我假设我们已经在网页上搜索引擎,并在第一页中找到了所有的网站(我在DuckDuckGo上的搜索词是:dark web“.onion”) 从这里开始,代码就是这样运行的(我将在代码注释中详细说明事情): 所以我的代码工作得很好。但有一个简单的问题。正则表达式没有正确过滤所有

我的项目的目的是
webscrap
a搜索引擎(我选择了
DuckDuckGo
)。要获取第一页上的所有链接,然后输入其中的每个链接,请获取
HTML
源代码并执行正则表达式,该表达式将过滤HTML代码中的所有
.onion
网站

从这里开始,我假设我们已经在网页上搜索引擎,并在第一页中找到了所有的网站(我在DuckDuckGo上的搜索词是:dark web“.onion”)

从这里开始,代码就是这样运行的(我将在代码
注释中详细说明事情):

所以我的代码工作得很好。但有一个简单的问题。正则表达式没有正确过滤所有内容。一些HTML代码嵌套在my.onion网站中。而且,我经常在输出中单独获得
.onion

以下是输出的示例:

href="http://jv7aqstbyhd5hqki.onion
class="external_link">http://jv7aqstbyhd5hqki.onion
href="http://xdagknwjc7aaytzh.onion
data-qt-tooltip="xdagknwjc7aaytzh.onion
">http://xdagknwjc7aaytzh.onion
href="http://sbforumaz7v3v6my.onion
class="external_link">http://sbforumaz7v3v6my.onion
href="http://kpmp444tubeirwan.onion
class="external_link">http://kpmp444tubeirwan.onion
href="http://r5c2ch4h5rogigqi.onion
class="external_link">http://r5c2ch4h5rogigqi.onion
href="http://hbjw7wjeoltskhol.onion
class="external_link">http://hbjw7wjeoltskhol.onion
href="http://khqtqnhwvd476kez.onion
class="external_link">http://khqtqnhwvd476kez.onion
href="http://jahfuffnfmytotlv.onion
class="external_link">http://jahfuffnfmytotlv.onion
href="http://ocu3errhpxppmwpr.onion
class="external_link">http://ocu3errhpxppmwpr.onion
href="http://germanyhusicaysx.onion
data-qt-tooltip="germanyhusicaysx.onion
">http://germanyhusicaysx.onion
href="http://qm3monarchzifkwa.onion
class="external_link">http://qm3monarchzifkwa.onion
href="http://qm3monarchzifkwa.onion
class="external_link">http://qm3monarchzifkwa.onion
href="http://spofoh4ucwlc7zr6.onion
data-qt-tooltip="spofoh4ucwlc7zr6.onion
">http://spofoh4ucwlc7zr6.onion
href="http://nifgk5szbodg7qbo.onion
class="external_link">http://nifgk5szbodg7qbo.onion
href="http://t4is3dhdc2jd4yhw.onion
class="external_link">http://t4is3dhdc2jd4yhw.onion

我想知道如何改进此
regex
,以便获得正确格式的
.onion
链接

\S*?
的URL匹配模式太松散。它将匹配尽可能少的非空白字符以满足模式,模式包括

要了解URL中哪些字符有效,请参阅以下答案:


您可能可以通过类似于
[^\s]
而不是
\s
的方式逃脱惩罚
[^\s]
将匹配任何非空白或大括号字符,而不是匹配任何非空白字符。

您可以使用此正则表达式。它与.onion
它在源html上工作,获取/测试任何标记的href属性

您不需要使用正则表达式选项,因为它们包含在内联中。
您需要的是
捕获组3

r“(?si)\”]|\“[^\”]*\“[^']*\”)*?(?]*?)+
>

谢谢你的建议!我尝试用
[^\S]
替换
\S
,并删除了一些不需要的部分。但大部分仍然存在,如
href=”http://lw4ipk5choakk5ze.onion
我也得到了很多
.onion/.onion
甚至像
16这样的东西。http://nr6juudpp4as4gjg.onion
我将寻找更好的解决方法。感谢您提供的链接!
href="http://jv7aqstbyhd5hqki.onion
class="external_link">http://jv7aqstbyhd5hqki.onion
href="http://xdagknwjc7aaytzh.onion
data-qt-tooltip="xdagknwjc7aaytzh.onion
">http://xdagknwjc7aaytzh.onion
href="http://sbforumaz7v3v6my.onion
class="external_link">http://sbforumaz7v3v6my.onion
href="http://kpmp444tubeirwan.onion
class="external_link">http://kpmp444tubeirwan.onion
href="http://r5c2ch4h5rogigqi.onion
class="external_link">http://r5c2ch4h5rogigqi.onion
href="http://hbjw7wjeoltskhol.onion
class="external_link">http://hbjw7wjeoltskhol.onion
href="http://khqtqnhwvd476kez.onion
class="external_link">http://khqtqnhwvd476kez.onion
href="http://jahfuffnfmytotlv.onion
class="external_link">http://jahfuffnfmytotlv.onion
href="http://ocu3errhpxppmwpr.onion
class="external_link">http://ocu3errhpxppmwpr.onion
href="http://germanyhusicaysx.onion
data-qt-tooltip="germanyhusicaysx.onion
">http://germanyhusicaysx.onion
href="http://qm3monarchzifkwa.onion
class="external_link">http://qm3monarchzifkwa.onion
href="http://qm3monarchzifkwa.onion
class="external_link">http://qm3monarchzifkwa.onion
href="http://spofoh4ucwlc7zr6.onion
data-qt-tooltip="spofoh4ucwlc7zr6.onion
">http://spofoh4ucwlc7zr6.onion
href="http://nifgk5szbodg7qbo.onion
class="external_link">http://nifgk5szbodg7qbo.onion
href="http://t4is3dhdc2jd4yhw.onion
class="external_link">http://t4is3dhdc2jd4yhw.onion
r"(?si)<[\w:]+(?=(?:[^>\"']|\"[^\"]*\"|'[^']*')*?(?<=\s)href\s*=\s*(?:(['\"])\s*(((?!mailto:)(?:(?:https?|ftp)://)?(?:(?:(?!\1)\S)+(?::(?:(?!\1)\S)*)?@)?(?:(?:[a-z\u00a1-\uffff0-9]-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-?)*[a-z\u00a1-\uffff0-9]+)*?\.onion\b)(?:(?!\1).)*?)\s*\1))\s+(?:\".*?\"|'.*?'|[^>]*?)+>"
 (?si)                         # Dot-all and case insensitive modifiers
 < [\w:]+                      # Any tag
 (?=
      (?: [^>"'] | " [^"]* " | ' [^']* ' )*?
      (?<= \s )
      href \s* = \s*                # href attribute
      (?:
           ( ['"] )                      # (1)
           \s* 
           (                             # (2 start), Full url
                (                             # (3 start), The url up to '.onion'
                     (?! mailto: )
                     (?:
                          (?: https? | ftp )
                          ://
                     )?
                     (?:
                          (?:
                               (?! \1 )
                               \S 
                          )+
                          (?:
                               : 
                               (?:
                                    (?! \1 )
                                    \S 
                               )*
                          )?
                          @
                     )?
                     (?:
                          (?: [a-z\u00a1-\uffff0-9] -? )*
                          [a-z\u00a1-\uffff0-9]+ 
                     )
                     (?:
                          \.
                          (?: [a-z\u00a1-\uffff0-9] -? )*
                          [a-z\u00a1-\uffff0-9]+ 
                     )*?
                     \.onion                        \b 
                )                             # (3 end)
                (?:                           # Parameters
                     (?! \1 )
                     . 
                )*?
           )                             # (2 end)
           \s* \1 
      )
 )
 \s+ 
 (?: " .*? " | ' .*? ' | [^>]*? )+
 >