On 2017-05-10 20:25, aaron.m.weisb...@gmail.com wrote:
Good afternoon,
I have a list that I'm iterating thorough in Python. Each item in the list will have
between 1-200 urls associated with it. My goal is to move on to the next. I have a
variable "associationsCount" that is counting the number of urls and once it
gets to 0 i want to move on to the next item on the list and do the same thing.
As my code stands right now, it counts through the stateList[0] and then when
associationsCount gets to 0 it just stops. I know I need to add another couple
lines of code to get it back going again- but I'm not no good at no Python.
Please advise.
Here's an example of the code:
stateList = ['AL','AK','AR','AZ',etc]
associationsCount = 1
for state in stateList:
while associationsCount > 0:
What is 'counter'? It only ever increases.
print(counter)
I wonder whether it's supposed to increase from, say, 1, for each state,
to get each of a number of pages for each state. (Pages 1, 2, etc. for
'AL', then pages 1, 2, etc. for 'AK', and so on? If that's the case,
then how do you know when you have seen the last page?)
url = 'url?dp={0}&n=&s={1}&c=&z=&t1=&g='.format(counter,state)
print(url)
page = requests.get(url)
tree = html.fromstring(page.text)
s = s + counter
counter +=1
associations = tree.xpath('//td//strong/text()')
You're not doing much with 'associationsCount'. Once set, you never
change it, so the 'while' loop will repeat forever - unless it happens
to be set to 0, in which case the 'while' loop will never run again!
associationsCount = len(associations)
print(associationsCount)
for x in associations:
print(x)
xUrl = 'https://bing.com/search?q={0}'.format(x)
xPage = requests.get(xUrl)
xTree = html.fromstring(xPage.text)
try:
link = xTree.xpath('//li[@class="b_algo"]//a/@href')[0]
print(link)
associationInfo = state, x,link
associationInfoList.append(associationInfo)
NEVER use a 'bare except' to suppress exceptions! It'll catch _all_
exceptions, even NameError (if you've misspelled a name, it'll catch
that too). Catch only those exceptions that you're prepared to deal with.
except:
pass
--
https://mail.python.org/mailman/listinfo/python-list