1

我有一个代码,它遍历 url 列表以执行一些操作,但输入的 url 必须每个都包含查询字符串,我想首先检查 url 是否正确并且实际上包含查询字符串,我搜索了大部分正则表达式我发现只检查 url,我找到的最接近的解决方案是使用 urlparse 像这样

#!/usr/local/bin/python2.7

from urlparse import urlparse
line = "http://www.compileonlinecom/execute_python_online.php?q="
o = urlparse(line)
print o
# ParseResult(scheme='http', netloc='www.compileonlinecom',          path='/execute_python_online.php', params='', query='q=', fragment='')

if (o.scheme=='http' and o.query!=''):
print "yes , that is a url with query string  "

else:
   print "No match!!"

但我想知道是否可以使用更可靠的正则表达式来完成

4

1 回答 1

0

您可以尝试在问号上验证它,因为每个带有参数的 url 都应该在 url 中有一个问号。

例子:

sites = ['site.com/index.php?id=1', "xyz.com/sf.php?df=22", "dfd.com/sdgfdg.php?ereg=1", "normalsite.com"]
for site in sites:
    if "?" in site:
         print site

结果:

site.com/index.php?id=1
xyz.com/sf.php?df=22
dfd.com/sdgfdg.php?ereg=1

您会看到没有参数的站点尚未打印。

于 2014-05-07T02:22:17.863 回答