Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.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 urllib模块:在URL上迭代_Python_Urllib - Fatal编程技术网

Python urllib模块:在URL上迭代

Python urllib模块:在URL上迭代,python,urllib,Python,Urllib,我正在尝试从页面的html文本中提取最后5个字符,并使用它们替换url中的最后5个字符,然后重试。我需要重复几次 这就是我想到的。目前,它连续5次打印相同的url import urllib.request prevurl = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345" for i in range(1,5): with urllib.request.urlopen(prevurl) as u

我正在尝试从页面的html文本中提取最后5个字符,并使用它们替换url中的最后5个字符,然后重试。我需要重复几次

这就是我想到的。目前,它连续5次打印相同的url

import urllib.request

prevurl = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345"
for i in range(1,5):
    with urllib.request.urlopen(prevurl) as url:
        s = url.read()
        prevurl.replace('[-5:]', 's[-5:]')
    print(prevurl)

我不明白为什么会有否定。我可以用一些建设性的批评来代替。鼓励学习的好方法

不管怎样,我想我知道了。它需要一些额外的步骤,但工作方式正是我想要的

import urllib.request

prevurl = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345"
for i in range(1,400):
    with urllib.request.urlopen(prevurl) as url:
        s = url.read().decode("utf-8")
        n1 = []
        u1 = []
        for i in s:
            if i.isdigit():
                n1.append(i)
                n2 = ''.join(n1)
        for i in prevurl:
            if i.isdigit():
                u1.append(i)
                u2 = ''.join(u1)
        if len(n2) != len(u2):
            prevurl = prevurl.replace(prevurl[-(len(u2)):], n2)
        else:
            prevurl = prevurl.replace(prevurl[-(len(n2)):],n2)

    print(prevurl)

嗯。。。检查您要替换的内容。。。此处不应包含任何引号字符。删除引号似乎会导致语法无效。请尝试此
prevurl=prevurl.replace(prevurl[:-5],s[-5:])
New error。TypeError:replace()参数2必须是str,而不是bytes。我必须先转换
s
吗?