python csv importing to link
Question: I am trying to get Python to open sites based on a csv file. I checked all of
my code individually to make sure it worked and it does but when I introduce
this variable from the csv file I am getting the error message below: Here is
the code:

    
    
    import urllib
    import urllib.request
    from bs4 import BeautifulSoup
    import os
    
    import csv
    
    f = open('gropn1.csv')
    csv_f = csv.reader(f)
    
    for row in csv_f:
    
        theurl="http://www.grote.com/?s="+csv_f[1] + "&q1=1"
        thepage = urllib.request.urlopen(theurl)
        soup = BeautifulSoup(thepage,"html.parser")
    
        for partno in soup.find('h2',{"class":"single-product-number"}):
            print(partno)
    
        for link in soup.find('ul',{"class":"breadcrumbs"}).findAll('a'):
            print(link.text)
    
    
    
    f.close()
    

Here is the error:

    
    
    Traceback (most recent call last):
      File "grotestart2.py", line 13, in <module>
        theurl="http://www.grote.com/?s="+csv_f[1] + "&q1=1"
    TypeError: '_csv.reader' object is not subscriptable
    

Any help would be greatly appreciated! Thanks


Answer: > TypeError: '_csv.reader' object is not subscriptable

`csv_f` is your _csv reader instance_ and it is actually ["not
subscriptable"](http://stackoverflow.com/questions/216972/in-python-what-does-
it-mean-if-an-object-is-subscriptable-or-not) by definition.

Did not you mean to use the `row` variable instead. Replace:

    
    
    theurl="http://www.grote.com/?s="+csv_f[1] + "&q1=1"
    

with:

    
    
    theurl="http://www.grote.com/?s="+row[1] + "&q1=1"
    

* * *

You are also trying to iterate over the results of `soup.find()` call which is
a `Tag` instance which is _not iterable._ You meant to use `find_all()`.
Replace:

    
    
    for partno in soup.find('h2',{"class":"single-product-number"}):
    

with:

    
    
    for partno in soup.find_all('h2', {"class":"single-product-number"}):
    

Or, a shorter version using a [CSS
selector](http://www.crummy.com/software/BeautifulSoup/bs4/doc/#css-
selectors):

    
    
    for partno in soup.select('h2.single-product-number'):
    

