xrange和list迭代的问题

2024-06-16 10:18:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图遍历通过在requests模块中拆分响应字符串创建的列表,我的目标是操作并将捕获的数据添加到一个集合中;xrange中的每一页应该正好有我要查找的值的40,但是我的代码似乎是取每个迭代的最后一个值并将其添加到列表中,而不是每个值。因此,应该执行某些字符串加法(如:'http://example.com' + link1 + '.html', 'http://example.com' + link2 + '.html', 'http://example.com' + link3 + '.html', ...)的循环将返回不需要的子字符串(如:'http://example.com' + 'l' + '.html', 'http://example.com' + 'i' + '.html', 'http://example.com' + 'n' + '.html' , ...)。我如何才能改变这一点来实现目标,为什么会发生这种情况。你知道吗

last_pg = 10
    BASE_URL = 'http://example.com?act=view&NowPage=%s'
    urls = set()
    for i in xrange(last_pg):
        response = requests.get(BASE_URL % i)
        parsed_body = html.fromstring(response.text)
        links = response.text.split('-p-')[-1].split('-cat-')[0]
        print links #this seems to print the last value of each iteration rather than all of them
        for link in links:# this loop breaks down each link value into substrings and performs the interpolation on the substrings
            finallink = ('http://example.com-' + link.encode('ascii', 'ignore') + '.html')
            urls.add(finallink)
            print "added %s to que" % finallink
    print urls
    print len(urls)

Tags: the字符串comhttp列表exampleresponsehtml
1条回答
网友
1楼 · 发布于 2024-06-16 10:18:26

分割将返回一个列表,但是您将使用该列表的索引来执行第二次分割,因此您只能从中获得一个元素。response.text.split('-p-')给出一个列表,但是response.text.split('-p-')[-1]给出该列表的最后一个元素。如果你做了这样的事情:

links = [x.split('-cat-')[0] for x in response.split('-p-')] 

你可能会得到一个你想要的列表,但是你可能需要做更多的处理,要么改变你从'-cat-'分割得到的索引,要么通过从该分割得到的列表进行另一次迭代。你知道吗

之所以只得到单个字母,是因为遍历的是字符串,而不是字符串列表,所以它从字符串中生成字符,而不是单个字符串。你知道吗

相关问题 更多 >