0

我正在使用下面的代码来检查服务器响应代码。我不想手动输入 URL,而是希望 python 检查 CSV ( data.csv),然后将结果导出到新的 CSV ( new_data.csv)。有谁知道怎么写这个?

谢谢你的时间!

import urllib2
for url in ["http://stackoverflow.com/", "http://stackoverflow.com/questions/"]:
try:
    connection = urllib2.urlopen(url)
    print connection.getcode()
    connection.close()
except urllib2.HTTPError, e:
    print e.getcode()

# Prints:
#200 or 404

更新:

import csv

out=open("urls.csv","rb")
data=csv.reader(out)
data=[row for row in data]
out.close()

print data

import urllib2
for url in ["http://stackoverflow.com/", "http://stackoverflow.com/questions/"]:
try:
    connection = urllib2.urlopen(url)
    print connection.getcode()
    connection.close()
except urllib2.HTTPError, e:
    print e.getcode()

输出:

[['link'], ['link'], ['link'], ['link'], ['link'], ['link']]

200

200

更新:

import csv

with open("urls.csv", 'r') as csvfile:
    urls = [row[0] for row in csv.reader(csvfile)]

import urllib2
for url in urls:
    try:
        connection = urllib2.urlopen(url)
        print connection.getcode()
        connection.close()
    except urllib2.HTTPError, e:
        print e.getcode()
4

1 回答 1

0

我认为您从打印数据输出中获得了线索: [['link'], ['link'], ['link'], ['link'], ['link'], ['link']] -这告诉我,您可能在 data=[row for row in data] 行上犯了一个错误,因为它为您提供了一个列表列表,这就是为什么您不能简单地使用for url in data:.

顺便说一句,如果您对命名进行一些思考,您会发现整个事情就不那么令人困惑了 - 例如,来自名为“ out ”的文件句柄的输入data = 基于数据的东西......

于 2013-07-14T07:23:07.103 回答