Python urllib 模块:遍历 URL

Python urllib module: iterating over URL

我正在尝试从页面的 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)