0

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

PatrickP76
  • 135
  • 2
  • 11

1 Answers1

0

TypeError: '_csv.reader' object is not subscriptable

csv_f is your csv reader instance and it is actually "not subscriptable" 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:

for partno in soup.select('h2.single-product-number'):
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Yes the worked - but now I am getting another error: Traceback (most recent call last): File "C:/Users/PPluck/Downloads/grotestart2.py", line 17, in for partsno in soup.find('h2',{"class":"single-product-number"}): TypeError: 'NoneType' object is not iterable - Any ideas? – PatrickP76 Feb 16 '16 at 03:52