Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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随机URL选择_Python - Fatal编程技术网

Python随机URL选择

Python随机URL选择,python,Python,我正在尝试使用choice从这个列表中选择一个随机URL,但它不起作用。这是我的密码: import urllib, urllib2, sys num = sys.argv[1] print 'Started' phones = [ 'http://1.1.1.6/index.htm,' 'http://1.1.1.5/index.htm,' 'http://1.1.1.4/index.htm,' 'http://1.1.1.3/index.htm,' 'http://1.1.1.2/index

我正在尝试使用choice从这个列表中选择一个随机URL,但它不起作用。这是我的密码:

import urllib, urllib2, sys
num = sys.argv[1]
print 'Started'
phones = [
'http://1.1.1.6/index.htm,'
'http://1.1.1.5/index.htm,'
'http://1.1.1.4/index.htm,'
'http://1.1.1.3/index.htm,'
'http://1.1.1.2/index.htm,'
'http://1.1.1.1/index.htm'
]
from random import choice
data = urllib.urlencode({"NUMBER":num, "DIAL":"Dial", "active_line":1})
while 1:
    for phone in phones:

                         urllib2.urlopen(choice(phone),data) # make call
                         urllib2.urlopen(choice(phone)+"?dialeddel=0") # clear
logs
这就是我得到的错误

File "p.py", line 21, in ?
    urllib2.urlopen(choice(phone),data) # make call
  File "/usr/lib64/python2.4/urllib2.py", line 130, in urlopen
    return _opener.open(url, data)
  File "/usr/lib64/python2.4/urllib2.py", line 350, in open
    protocol = req.get_type()
  File "/usr/lib64/python2.4/urllib2.py", line 233, in get_type
    raise ValueError, "unknown url type: %s" % self.__original
ValueError: unknown url type: 5

感谢您的帮助。谢谢

逗号在字符串中。因此,phones变量是单个大字符串的列表。您的随机选择是从该字符串中选择一个字符。将其更改为:

phones = [
    'http://1.1.1.6/index.htm',
    'http://1.1.1.5/index.htm',
    'http://1.1.1.4/index.htm',
    'http://1.1.1.3/index.htm',
    'http://1.1.1.2/index.htm',
    'http://1.1.1.1/index.htm',
]
此外,您不应该在电话上进行迭代,只需使用
random.choice(电话)
选择一部电话即可

另外,您正在为两个URL呼叫选择不同的随机电话,我猜这不是您想要的。这是一个完整的重构代码

import urllib, urllib2, sys, random

phones = [
    'http://1.1.1.6/index.htm',
    'http://1.1.1.5/index.htm',
    'http://1.1.1.4/index.htm',
    'http://1.1.1.3/index.htm',
    'http://1.1.1.2/index.htm',
    'http://1.1.1.1/index.htm',
]

num = sys.argv[1]
data = urllib.urlencode({"NUMBER": num, "DIAL": "Dial", "active_line": 1})
while 1:
    phone = random.choice(phones)
    urllib2.urlopen(phone, data) # make call
    urllib2.urlopen(phone + "?dialeddel=0") # clear logs

逗号在字符串中。因此,phones变量是单个大字符串的列表。您的随机选择是从该字符串中选择一个字符。将其更改为:

phones = [
    'http://1.1.1.6/index.htm',
    'http://1.1.1.5/index.htm',
    'http://1.1.1.4/index.htm',
    'http://1.1.1.3/index.htm',
    'http://1.1.1.2/index.htm',
    'http://1.1.1.1/index.htm',
]
此外,您不应该在电话上进行迭代,只需使用
random.choice(电话)
选择一部电话即可

另外,您正在为两个URL呼叫选择不同的随机电话,我猜这不是您想要的。这是一个完整的重构代码

import urllib, urllib2, sys, random

phones = [
    'http://1.1.1.6/index.htm',
    'http://1.1.1.5/index.htm',
    'http://1.1.1.4/index.htm',
    'http://1.1.1.3/index.htm',
    'http://1.1.1.2/index.htm',
    'http://1.1.1.1/index.htm',
]

num = sys.argv[1]
data = urllib.urlencode({"NUMBER": num, "DIAL": "Dial", "active_line": 1})
while 1:
    phone = random.choice(phones)
    urllib2.urlopen(phone, data) # make call
    urllib2.urlopen(phone + "?dialeddel=0") # clear logs

您可以尝试获取一个随机索引

import random
phones = [
'http://1.1.1.6/index.htm',
'http://1.1.1.5/index.htm',
'http://1.1.1.4/index.htm',
'http://1.1.1.3/index.htm',
'http://1.1.1.2/index.htm',
'http://1.1.1.1/index.htm',
]

index random.randrange(0, len(phones)-1)
phones[index]

您可以尝试获取一个随机索引

import random
phones = [
'http://1.1.1.6/index.htm',
'http://1.1.1.5/index.htm',
'http://1.1.1.4/index.htm',
'http://1.1.1.3/index.htm',
'http://1.1.1.2/index.htm',
'http://1.1.1.1/index.htm',
]

index random.randrange(0, len(phones)-1)
phones[index]

选择(“你好”)->一个字母。。。choice(电话)将只返回URLCOICE的一个字母('hello')->一个字母。。。choice(phone)将只返回我使用过的URL的一个字母,并且它第一次使用随机字符成功地工作,但此后,手机不再呼叫。使用旧代码,它会一直呼叫,直到我停止,但只有列表中的第一部手机。我使用过此代码,第一次使用随机键成功工作,但此后,手机不再呼叫。使用旧代码,它会一直呼叫,直到我停止,但只有列表中的第一个电话。