Python URL 步进只返回第一页结果 [英] Python URL Stepping Returns Only First Page Results
问题描述
对以下代码的任何帮助将不胜感激.我已经使用 print 检查了 h 和 g 的结果,以验证它们是否正确增加了 url,但该程序似乎只是重复了第一页的结果.希望这是有道理的,我已经提供了足够的信息.我知道这段代码看起来很糟糕.
Any help with the below code would be appreciated. I have checked the results of h and g using print to verify that they are incrementing the url properly, but the program seems to be only repeating the results from first page. Hope this makes sense and I have provided enough info. I know this code looks terrible.
edit** 我在 Python2.7 shell 中测试代码.我让它打印链接结果以确保它们正常工作,但它只是重复 pg1.
edit** I am testing the code as I go in the Python2.7 shell. I have it print the link results to make sure they are working correctly, but it just repeats pg1.
更新** 代码的问题是由于网站使用json获取页面.Python 文件迭代器链接不迭代
Update** The problem with the code was due to the website using json to get pages. Python Link to File Iterator not Iterating
g = 'http://www.somesite.com/pg'
b = 'http://www.somesite.com/pg'
PageCount = 1
while PageCount < 3:
h = g + str(PageCount)
c = b + str(PageCount)
f = urllib2.urlopen(h)
# variable a is for the second function that opens links for webpages
# meeting criteria from variable f
a = urllib2.urlopen(c)
# res variable captures lines for items meeting criteria to be opened in a webpage
res = []
PageCount += 1
#check function checks for criteria current webpage
check()
#ReturnLine function opens webpages using data from variable res
ReturnLine()
推荐答案
我导出了一个最小的工作示例(注释部分没有冗长的代码......)
I derived a minimal working example (no lengthy code possible in comment section..)
g = 'http://www.somesite.com/pg'
PageCount = 1
while PageCount < 3:
h = g + str(PageCount)
print h
PageCount += 1
这工作得很好.输出是
http://www.somesite.com/pg1
http://www.somesite.com/pg2
这是你得到的吗?如果是这样,请尝试使用固定 url 调用 urllib2.urlopen([URL]) 以检查 单独 最小工作示例中的正确功能,然后从那里开始.否则,我看不到可能导致此类行为的错误(或错误来源).
Is this what you get? If so, try calling urllib2.urlopen([URL]) with a fixed url to check proper function in a separate minimal working example and go from there. Otherwise I see no error (or sources of error) that may cause such a behaviour.
这篇关于Python URL 步进只返回第一页结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!