pgno = 1
while pgno < 4304:
result = urllib.urlopen("http://www.example.comtraderesourcespincode.aspx?" +
"&GridInfo=Pincode0"+ pgno)
print pgno
html = result.read()
parser = etree.HTMLParser()
tree = etree.parse(StringIO.StringIO(html), parser)
pgno += 1
in http://.......=Pincode0
I need to add 1..for e.g like 'Pincode01', loop it 01 to 02, 03 .. for which I am using a while loop and the variable assigned is 'pgno'.
The problem is the counter is adding 1, but 'Pincode01' is not becoming 'Pincode02' ... therefore it is not opening the 2nd page of the site.
I even tried +str(pgno))
... no luck.
Please show how to do it. I am not able to do this ...and have attempted it several times.
The loop:
Works correctly and the number is increasing.
You are either describing the problems in incorrect way or there is problem in your basic assumptions of the problem. Can you please try to describe what you are trying to do in the first place?
If your problem is with the format of the number use this instead of adding a str to an int:
See the string format docs for more options
Also, in a more pythonic way using string format
Probably, you want this :
result
I wrote this code after having studied the structure of the HTML source code with the following code (I think you'll understand it without any more explanations):
.
Now, the problem is that the same HTML is returned for all the values of pgno. The site may detect it is a program that wants to connect and fetch data. This problem must be treated with the tools in urllib2, but I'm not trained to that.