使用urllib2.urlopen()时未知的URL类型

0 投票
2 回答
2457 浏览
提问于 2025-04-18 06:38

我想做以下几件事:

  • 打开一个包含网址列表的CSV文件(也就是GET请求)
  • 读取这个CSV文件,把里面的内容写入一个列表
  • 逐个打开每个网址,读取返回的内容
  • 把这些返回的内容写回到一个新的CSV文件里

我遇到了以下错误:

Traceback (most recent call last):
  File "C:\Users\l.buinui\Desktop\request2.py", line 16, in <module>
    req = urllib2.urlopen(url)
  File "C:\Python27\lib\urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "C:\Python27\lib\urllib2.py", line 404, in open
    response = self._open(req, data)
  File "C:\Python27\lib\urllib2.py", line 427, in _open
    'unknown_open', req)
  File "C:\Python27\lib\urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 1247, in unknown_open
    raise URLError('unknown url type: %s' % type)
URLError: <urlopen error unknown url type: ['http>

这是我正在使用的代码:

import urllib2
import urllib
import csv

# Open and read the source file and write entries to a list called link_list
source_file=open("source_new.csv", "rb")
source = csv.reader(source_file, delimiter=";")
link_list = [row for row in source]
source_file.close()

# Create an output file which contains the answer of the GET-Request
out=open("output.csv", "wb")
output = csv.writer(out, delimiter=";")
for row in link_list:
    url = str(row)
    req = urllib2.urlopen(url)
    output.writerow(req.read())

out.close()

这里出了什么问题呢?

提前谢谢你们的任何提示。

祝好

2 个回答

0

现在可以正常工作了。如果我在循环中直接引用 row[0],就没有问题。

import urllib2
import urllib
import csv

# Open and read the source file and write entries to a list called link_list
source_file=open("source.csv", "rb")
source = csv.reader(source_file, delimiter=";")
link_list = [row for row in source]
source_file.close()

# Create an output file which contains the answer of the GET-Request
out=open("output.csv", "wb")
output = csv.writer(out)
for row in link_list:
    req = urllib2.urlopen(row[0])
    answer = req.read()
    output.writerow([answer])


out.close()
0

使用row这个变量会把一个列表元素(里面只有一个元素,就是网址)传给urlopen,但如果传row[0],就会直接传一个包含网址的字符串。

csv.reader每读取一行数据都会返回一个列表,不管这一行里面有多少个项目。

撰写回答