urllib.openurl将http链接视为本地文件地址

时间:2018-03-23 16:30:52

标签: python python-2.7 urllib

尝试使用csv的视频链接和精美的汤来为youtube视频节目编写一个非常基本的刮刀。目前的脚本是:

#!/usr/bin/python

from bs4 import BeautifulSoup
import urllib
import csv

with open('url-titles-list.csv', 'wb') as csv_out:
    fieldnames = ['url', 'title']
    writer = csv.DictWriter(csv_out, fieldnames = fieldnames)
    with open('url-nohttps-list.csv', 'rb') as csv_in:
        reader = csv.DictReader(csv_in, fieldnames=['linkurls'])
        writer.writeheader()
        for row in reader:
            link = row['linkurls']
            with urllib.urlopen(link) as response:
                html = response.read()
                soup = BeautifulSoup(html, "html.parser")
                name = soup.title.string
                writer.writerow({'url': row['linkurls'], 'title': name})

这在urllib.urlopen(link)处中断,使用以下回溯使得它看起来像url类型未被正确识别,并且它试图打开链接作为本地文件?

Traceback (most recent call last):
  File "/Users/clarapouletty/Desktop/operation_find_yuzusho/fetcher.py", line 15, in <module>
    with urllib.urlopen(link) as response:
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 87, in urlopen
    return opener.open(url)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 213, in open
    return getattr(self, name)(url)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 469, in open_file
    return self.open_local_file(url)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 483, in open_local_file
    raise IOError(e.errno, e.strerror, e.filename)
IOError: [Errno 2] No such file or directory: 'linkurls'

Process finished with exit code 1

非常感谢任何帮助!

0 个答案:

没有答案