使用urllib调用文件夹

时间:2012-01-16 17:10:24

标签: python urllib

我是python的新手。我的桌面上有一个文件夹,在该文件夹中我有一些http链接。我的任务是打开桌面上的该文件夹。我需要调用该文件夹,调用后我需要打开该链接并执行一些操作。

  • 如何使用python program和urllib
  • 调用此文件夹
  • 我可以在urllib中提供xpath,我该怎么做呢...

如果可能,任何人都可以为此提供xpath示例..

任何人都可以帮助我。

1 个答案:

答案 0 :(得分:1)

第一部分请看OS模块。

正如Thomas建议:

  

os.listdir()将获取目录中的文件,内置的open函数将读取文件。

对于问题的第二部分,您需要使用第三方库[html / xml解析器](如lxmlBeautiful Soup和urllib来使用XPath

示例:

import lxml
from lxml import etree
import urllib2

response = urllib2.urlopen('url here')
html = response.read()
data = etree.HTML(html)
contents = data.xpath('xpath here')

您也可以在没有urllib的情况下执行此操作:

import lxml.html
doc = lxml.html.parse('your url')
contents  = doc.xpath('xpath expr here')
相关问题