【发布时间】:2017-07-12 06:34:37
【问题描述】:
我正在尝试根据搜索词从网站中提取链接并将它们写入 csv 文件。该文件已创建,但链接未写入其中。我得到错误 - 字符串索引必须是整数,而不是 str。
无法弄清楚我的错误。
from pprint import pprint
import requests
import lxml
import csv
from bs4 import BeautifulSoup
def get_url_for_search_key(search_key):
base_url = 'http://www.marketing-interactive.com/'
response = requests.get(base_url + '?s=' + search_key)
soup = BeautifulSoup(response.content, "lxml")
return [url['href'] for url in soup.findAll('a', {'rel': 'bookmark'})]
pprint(get_url_for_search_key('digital marketing'))
with open('ctp_output.csv', 'wb') as f:
writer = csv.writer(f)
for url in get_url_for_search_key('digital marketing'):
writer.writerows( url['href'] )
【问题讨论】:
-
错误出现在哪一行?请发布完整的回溯。
-
最后一行出现错误——writer.writerows(url['href'])
标签: python csv web-scraping beautifulsoup