Iterating through a list. Upon condition I want to move on to next item in list
aaron.m.weisberg at gmail.com
aaron.m.weisberg at gmail.com
Wed May 10 15:25:14 EDT 2017
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:
print(counter)
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()')
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)
except:
pass
More information about the Python-list
mailing list