Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/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脚本没有正确返回页面源代码吗?_Python_Url_Urllib2 - Fatal编程技术网

为什么不是';我的Python脚本没有正确返回页面源代码吗?

为什么不是';我的Python脚本没有正确返回页面源代码吗?,python,url,urllib2,Python,Url,Urllib2,我刚刚编写了一个脚本,它旨在遍历字母表并查找所有无人认领的四个字母的twitter名称(实际上只是为了练习,因为我对Python还不熟悉)。我以前写过几个脚本,它们使用“urllib2”从url获取网站html,但这次它似乎不起作用。这是我的剧本: import urllib2 src='' url='' print "finding four-letter @usernames on twitter..." d_one='' d_two='' d_three='' d_four='' n_o

我刚刚编写了一个脚本,它旨在遍历字母表并查找所有无人认领的四个字母的twitter名称(实际上只是为了练习,因为我对Python还不熟悉)。我以前写过几个脚本,它们使用“urllib2”从url获取网站html,但这次它似乎不起作用。这是我的剧本:

import urllib2

src=''
url=''
print "finding four-letter @usernames on twitter..."
d_one=''
d_two=''
d_three=''
d_four=''
n_one=0
n_two=0
n_three=0
n_four=0
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

while (n_one > 26):
    while(n_two > 26):
        while (n_three > 26):
            while (n_four > 26):
                d_one=letters[n_one]
                d_two=letters[n_two]
                d_three=letters[n_three]
                d_four=letters[n_four]
                url = "twitter.com/" + d_one + d_two + d_three + d_four

                src=urllib2.urlopen(url)
                src=src.read()
                if (src.find('Sorry, that page doesn’t exist!') >= 0):
                    print "nope"
                    n_four+=1
                else:
                    print url
                    n_four+=1
            n_three+=1
            n_four=0
        n_two+=1
        n_three=0
        n_four=0
    n_one+=1    
    n_two=0
    n_three=0
    n_four=0
运行此代码返回以下错误:

SyntaxError:第29行文件名.py中的非ASCII字符“\xe2”, 但未声明编码;看见 详情

在访问该链接并进行其他搜索后,我在文档顶部添加了以下行:

# coding: utf-8
现在,虽然它不再返回错误,但似乎什么也没有发生。我加了一行

print src

它应该打印每个url的html,但在运行它时什么也没发生。任何建议都将不胜感激。

您可以使用
itertools.product

from itertools import product
for d_one, d_two, d_three, d_four in product(letters, repeat=4):
    ...
您可以使用
strings.ascii\u lowercase

您应该告诉urlopen您正在使用的协议(http)


另外,当您得到一个不存在的页面时,urlopen会引发一个
404
,因此您应该检查它,而不是查看页面文本那么,您可以初始化
n_one=0
,然后在(n_one>26)时执行一个循环
。当Python第一次遇到它时,它看到
,而(0>26)
,这显然是错误的,因此它跳过了整个循环


正如gnibbler的回答所告诉你的,无论如何,有更干净的方法来做循环。

那么第29行是什么?显然,上面的代码并不代表您的真实代码——否则我们会在上面的代码中看到特殊字符。下一票…第29行是“打印‘不’”。。。我发誓我五分钟前才写了这个脚本…仅供参考,这个脚本需要很长时间才能运行。有
26*26*26*26=456976
可能的四个字母单词。即使您能够每秒处理两次,您的脚本仍将花费
456976*0.5秒*(1分钟/60秒)*(1小时/60分钟)=大约63.47小时
。谢谢,是的,我知道。我将每隔一段时间运行它(首先,直到n_one>5,然后我将慢慢增加这个数字)。这比什么都重要,但我很欣赏这些想法。哦,太棒了!谢谢你的提示;我会实现这个。哇。你是完全正确的–他们应该是“非常感谢你指出了这一点并提供了快速帮助。”。
url = "http://twitter.com/" + d_one + d_two + d_three + d_four