简体   繁体   中英

python csv importing to link

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

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'):

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM