使用python访问网页内容

时间:2015-07-30 10:47:36

标签: python python-2.7

我正在尝试访问和操作网页的数据。 出于主要测试目的,我使用了w3schools网站,但我无法使用以下代码访问内容:

import urllib as url
import xml.etree.ElementTree as et

web_data = url.urlopen("www.w3schools.com/xml/cd_catalog.xml")
str_data = web_data.read()
print str_data

这些是我得到的错误:

Traceback (most recent call last):
  File "C:\Python27\webpage.htlm.py", line 4, in <module>
    web_data = url.urlopen("www.w3schools.com/xml/cd_catalog.xml")
  File "C:\Python27\lib\urllib.py", line 87, in urlopen
    return opener.open(url)
  File "C:\Python27\lib\urllib.py", line 213, in open
    return getattr(self, name)(url)
  File "C:\Python27\lib\urllib.py", line 469, in open_file
    return self.open_local_file(url)
  File "C:\Python27\lib\urllib.py", line 483, in open_local_file
    raise IOError(e.errno, e.strerror, e.filename)
IOError: [Errno 2] The system cannot find the path specified: 'www.w3schools.com\\xml\\cd_catalog.xml'

1 个答案:

答案 0 :(得分:4)

网址以协议开头。

在这里,您可能想要http://www.w3schools.com/xml/cd_catalog.xml