Scraping: add data stored as a picture to CSV file in python 3.5
<p>For this project, I am scraping data from a database and attempting to export this data to a spreadsheet for further analysis. (Previously posted <a href="http://stackoverflow.com/questions/39762565/export-beautifulsoup-scraping-results-to-csv-scrape-include-image-values-in-c">here</a>--thanks for the help over there reworking my code!)</p>

<p>I previously thought that finding the winning candidate in the table could be simplified by just always selecting the first name that appears in the table, as I thought the "winners" always appeared first. However, this is not the case. </p>

<p>Whether or not a candidate was elected is stored in the form of a picture in the first column. How would I scrape this and store it in a spreadsheet?</p>

<p>It's located under &lt; td headers > as:</p>

<pre><code>&lt;img src="/WPAPPS/WPR/Content/Images/selected_box.gif" alt="contestant won this nomination contest"&gt;
</code></pre>

<p>My question is: how would I use BeautifulSoup to parse the HTML table and extract a value from the first column, which is stored in the table as an image rather than text.</p>

<p>I had an idea for attempting some sort of Boolean sorting measure, but I am unsure of how to implement. </p>

<p>My code is below:</p>

<pre><code>from bs4 import BeautifulSoup
import requests
import re
import csv


url = "http://www.elections.ca/WPAPPS/WPR/EN/NC?province=-1&amp;distyear=2013&amp;district=-1&amp;party=-1&amp;pageno={}&amp;totalpages=55&amp;totalcount=1368&amp;secondaryaction=prev25"
rows = []

for i in range(1, 56):
    print(i)
    r  = requests.get(url.format(i))
    data = r.text
    cat = BeautifulSoup(data, "html.parser")
    links = []

    for link in cat.find_all('a', href=re.compile('selectedid=')):
        links.append("http://www.elections.ca" + link.get('href'))  

    for link in links:
        r  = requests.get(link)
        data = r.text
        cat = BeautifulSoup(data, "html.parser")
        lspans = cat.find_all('span')
        cs = cat.find_all("table")[0].find_all("td", headers="name/1")        
        elected = []

        for c in cs:
            elected.append(c.contents[0].strip())

        rows.append([
            lspans[2].contents[0], 
            lspans[3].contents[0], 
            lspans[5].contents[0],
            re.sub("[\n\r/]", "", cat.find("legend").contents[2]).strip(),
            re.sub("[\n\r/]", "",  cat.find_all('div', class_="group")[2].contents[2]).strip().encode('latin-1'),
            len(elected),
            cs[0].contents[0].strip().encode('latin-1')
            ])

with open('filename.csv', 'w', newline='') as f_output:
   csv_output = csv.writer(f_output)
   csv_output.writerows(rows)
</code></pre>

<p>Really--any tips would be GREATLY appreciated. Thanks a lot.</p>

<p>This snippet will print the name of the elected person:</p>

<pre><code>from bs4 import BeautifulSoup
import requests
req  = requests.get("http://www.elections.ca/WPAPPS/WPR/EN/NC/Details?province=-1&amp;distyear=2013&amp;district=-1&amp;party=-1&amp;selectedid=8548")
page_source = BeautifulSoup(req.text, "html.parser")
table = page_source.find("table",{"id":"gvContestants/1"})
for row in table.find_all("tr"):
    if not row.find("img"):
        continue
    if "selected_box.gif" in row.find("img").get("src"):
        print(''.join(row.find("td",{"headers":"name/1"}).text.split()))
</code></pre>

<p>As a side note please refrain yourself from declaring variables with meaningless names. It hurts the eyes of anyone trying to help you and it will hurt you in the future when looking at the code again</p>

python csv web-scraping beautifulsoup python-3.5