Nested for-loop and appending to empty objects
<p>I am providing values to a website filter In order to generate different html which l parse. I want to save each page source to a different Python object in order to distinguish the data. I have a list of empty objects which l will append to. parsing page source,and want to save each page source to its own Python object, which is itself in a list. In this way </p>

<p>The challenge is how to append the td elements from a particular html source, to the particular empty object in the list. I need to store html source at each iteration, in a separate object, which is itself found in a list.</p>

<p>I will simplify my example:</p>

<pre><code>years = ['2015', '2016]
weeks = ['1', '2']
store = [[], [], [], []]
</code></pre>

<p>This gives me 4 sets of html source that I need to capture:</p>

<pre><code>for y in years:
   for w in weeks:
</code></pre>

<p>#I will use y and w in webdriver.select to provide values for web page filter
I will then use BS to copy page source for each iteration:</p>

<pre><code>html = browser.page_source

soup = BeautifulSoup(html, "lxml")
</code></pre>

<p>And then iterate through the particular page source to extract td elements:</p>

<pre><code>counter = 0     
for el in soup.find_all('td'):
</code></pre>

<p>to provide index for store list in order to append td elements to separate empty objects</p>

<pre><code>for el in soup.find_all('td'):
        store[counter].append(el.get_text())
counter = counter + 1
</code></pre>

<p>Strip the element of html characters, and add to counter to move to the next object in the store list.</p>

<p>But the result is that all the td elements get appended to first object in the list instead of each html source having its own object. What am I missing? </p>

<p>Would it better to somehow use map function?</p>

<p>Your statement</p>

<pre><code>counter=counter+1
</code></pre>

<p>is not within the for loop.</p>

<p>You need to indent it at the same level as the previous line, so that counter is incremented each time around the loop</p>

python nested-loops