[英]python csv importing to link
我正在嘗試讓Python打開基於csv文件的站點。 我單獨檢查了所有代碼以確保它可以正常工作,但是當我從csv文件中引入此變量時,出現以下錯誤消息:這是代碼:
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()
這是錯誤:
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
任何幫助將不勝感激! 謝謝
TypeError:“ _ csv.reader”對象不可下標
csv_f
是您的csv閱讀器實例 ,根據定義,它實際上是“不可下標的” 。
您不是要改用row
變量。 更換:
theurl="http://www.grote.com/?s="+csv_f[1] + "&q1=1"
與:
theurl="http://www.grote.com/?s="+row[1] + "&q1=1"
您還嘗試對soup.find()
調用的結果進行迭代,這是一個不可迭代的Tag
實例。 您打算使用find_all()
。 更換:
for partno in soup.find('h2',{"class":"single-product-number"}):
與:
for partno in soup.find_all('h2', {"class":"single-product-number"}):
或者,使用CSS選擇器的較短版本:
for partno in soup.select('h2.single-product-number'):
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.