指向文件迭代器的Python链接未迭代

指向文件迭代器的Python链接未迭代,python,loops,python-2.7,urllib2,Python,Loops,Python 2.7,Urllib2,这个问题已经困扰了我好几天了,我相信我终于把它缩小到了这段代码。如果有人能告诉我如何解决这个问题,以及为什么会发生这样的事情,那就太棒了 import urllib2 GetLink = 'http://somesite.com/search?q=datadata#page' holder = range(1,3) for LinkIncrement in holder: h = GetLink + str(LinkIncrement) ReadLink = urllib2.

这个问题已经困扰了我好几天了,我相信我终于把它缩小到了这段代码。如果有人能告诉我如何解决这个问题,以及为什么会发生这样的事情,那就太棒了

import urllib2

GetLink = 'http://somesite.com/search?q=datadata#page'
holder = range(1,3)

for LinkIncrement in holder:
    h = GetLink + str(LinkIncrement)
    ReadLink = urllib2.urlopen(h)
    f = open('test.txt', 'w')

    for line in ReadLink:
        f.write(line)  

    f.close()
    main() #calls function main that does stuff with the file
    continue
问题是它只会从
写入数据http://somesite.com/search?q=datadata#page“
如果我执行以下操作,结果将正确打印

for LinkIncrement in holder:
    h = GetLink + str(LinkIncrement)
    print h
我复制的链接确实以这种方式增加,我能够通过复制和粘贴打开URL。此外,我还尝试了
while
循环,但总是得到相同的结果

下面的代码打开3个选项卡,其中URL递增
/search?q=datadata#page1
/search?q=datadata#page2
,以及
/search?q=datadata#page3
。只是在我的代码中无法工作

import webbrowser
import urllib2
h = ''
def tab(passed):
    url = passed
    webbrowser.open_new_tab(url + '/')

def test():

    g = 'http://somesite.com/search?q=datadata#page'
    f = urllib2.urlopen(g)      
    NewVar = 1
    PageCount = 1

    while PageCount < 4:

            h = g + str(NewVar)                  
            PageCount += 1
            NewVar += 1
            tab(h)
test()
导入网络浏览器
导入urllib2
h=“”
def选项卡(已通过):
url=已通过
webbrowser.打开“新建”选项卡(url+“/”)
def test():
g='http://somesite.com/search?q=datadata#page'
f=urlib2.urlopen(g)
NewVar=1
页面计数=1
当页面计数小于4时:
h=g+str(新变量)
页面计数+=1
NewVar+=1
制表符(h)
测试()

感谢Falsetru帮我解决了这个问题。该网站对第一页之后的任何页面使用json。

在url中,
#
()之后的部分不会传递给web服务器;服务器响应相同的内容,因为framents标识符之前的部分相同


#某些东西
由浏览器(javascript)处理。你需要看看javascript中发生了什么。

我认为这可能也是OP的答案,只要这次我们确定确实有一个
#
。。。如果是这样,我希望我们知道下一个问题是什么……:)文件
test.txt
在每个循环中都会被覆盖,这可能是OP想要的,也可能不是OP想要的。我想你可能已经搞定了,但我只是用代码更新了我的问题,使之符合我的需要。当我把它放进我的代码里时,它就不能工作了@Matthias,我不在乎以文本形式保留每个链接。@Timmay,请参阅页面中包含的javascript代码。或者,调查浏览器调试工具提供的网络信息会更容易。@Timay,第二页可通过
http://steamcommunity.com/market/search/render/?query=appid%3A570%20common&search_descriptions=0&start=10&count=10
。第三个页面是
http://steamcommunity.com/market/search/render/?query=appid%3A570%20common&search_descriptions=0&start=20&count=10
。。。从开发者工具(Chrome)的网络选项卡中找到此信息。该位置中的URL不是我回答时传递给服务器的实际URL。