将字符串添加到URL的末尾

时间:2010-04-30 17:00:46

标签: python string url-rewriting httplib2

为了练习更多python,我一直在挑战pythonchallenge.com上的挑战

简而言之,作为第一步,这个挑战需要从最后用数字加载一个html页面。该页面包含一行文本,其中包含一个数字。该数字用于替换url中的现有数字,因此将您带到序列中的下一页。显然这种情况持续了一段时间......(这个挑战还有更多,但让这部分工作是第一步)。

我这样做的代码如下(限于暂时执行序列中前四页的内容)。由于某种原因,它第一次工作 - 它获取序列中的第二页,读取数字,转到第三页,并在那里读取数字。但后来它陷入了第三个问题。我不明白为什么,虽然认为这可能与我尝试将数字转换为字符串之前将其放在URL的末尾之间有关。要回答这个显而易见的问题,是的,我知道pythonchallenge正常工作 - 只要您有耐心,就可以手动进行网址编号,如果您愿意,可以确认:p

import httplib2
import re

counter = 0
new = '12345' #the number for the initial page in the sequence, as a string

while True:
    counter = counter + 1
    if counter == 5:
        break

    original = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='
    nextpage = original+new     #each page in the sequence is visited by adding 
                                #the number after 'nothing='
    print(nextpage)

    h = httplib2.Http('.cache')
    response, content = h.request(nextpage, "GET")  #get the content of the page, 
                                                    #which includes the number for the 
                                                    #*next* page in the sequence

    p = re.compile(r'\d{4,5}$')     #regex to find a 4 to 5 digit number at the end of
                                    #the content

    new = str((p.findall(content)))     #make the regex result a string - is this
                                            #where the problem lies?

    print('cached?', response.fromcache)    #I was worried my requests were somehow
                                            #being cached not actually sent afresh to
                                            #pythonchallenge. But it seems they aren't.

    print(content)
    print(new)

以下的输出如下。它似乎在第一次运行时工作正常(将92512添加到url并成功获取下一页并找到下一个值)但之后它只是卡住了,并且似乎没有在序列中加载以下页面。通过在浏览器中手动更改URL进行测试,确认数字正确并且pythonchallenge正常工作。

在我看来,有些事情出错了,将我的正则表达式搜索转换为字符串添加到URL的末尾 - 但为什么它应该第一次工作而不是第二次我不知道。我也担心我的请求可能只是缓存(我是httplib2的新手,并且不知道它是如何进行缓存的)但是它们似乎不是。我还在请求中添加了一个no-cache参数,以确保(未在此代码中显示),但它没有帮助。

  
    

http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345

         

('缓存?',错误)

         

,接下来没有什么是92512

         

[ '92512']

         

http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=[ '92512']

         

('缓存?',错误)

         

,接下来没有什么是72758

         

[ '72758']

         

http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=[ '72758']

         

('缓存?',错误)

         

,接下来没有什么是72758

         

[ '72758']

         

http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=[ '72758']

         

('缓存?',错误)

         

,接下来没有什么是72758

         

[ '72758']

  

如果有人能够指出我哪里出错,以及任何相关提示,我将不胜感激

提前致谢...

1 个答案:

答案 0 :(得分:1)

http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=['72758']
                                                             ^^     ^^

我想问题就在这里。 findall()返回一个列表:

  

re.findall(pattern,string [,flags])

     

返回字符串中pattern的所有非重叠匹配,作为字符串列表。从左到右扫描字符串,并按找到的顺序返回匹配项。如果模式中存在一个或多个组,则返回组列表;如果模式有多个组,这将是一个元组列表。结果中包含空匹配,除非它们触及另一场比赛的开头。

     

- Python doc