【问题标题】:Extract URLs with lxml使用 lxml 提取 URL
【发布时间】:2012-11-11 03:30:17
【问题描述】:

我已将一些 HTML 抓取到一个大的 txt 文件(约 50k 行)中,并希望提取一组特定的 URL。我追求的 URL 是两种模式之一:

第一

<div class="pic">
  <a href="https://www.site.com/joesmith"><img alt="Joe Smith" class="person_image" src="https://s3.amazonaws.com/photos.site.com/medium_jpg?12345678"></a>
</div>

第二

<div class="name">
  <a href="https://www.site.com/joesmith">Joe Smith</a>
</div>

我需要的文字是https://www.site.com/joesmith。我是第一次使用 lxml,我很难把它放在一起。

这是我的代码

from lxml import etree
from io import StringIO

def read(filename):
  file = open(filename, 'r')
  text = file.read()
  file.close()
  out = unicode(text, errors='ignore')
  return out

def parse(filename):
  data = read(filename)
  parser = etree.HTMLParser()
  tree = etree.parse(StringIO(data), parser)
  result = etree.tostring(tree.getroot(), pretty_print=True, method='HTML')
  urls = result.findall('<div class="name">')
  return urls

我用 findall 和 findtext 都试过这段代码,结果都是一样的,"AttributeError: 'str' object has no attribute 'findall'"。我已经确认“结果”是一个带有type() 的字符串。

我是否在正确的路径上提取 URL?我应该如何解决这个属性错误?

【问题讨论】:

    标签: python parsing extract lxml


    【解决方案1】:

    我不确定基于 HTML 的树是否支持 XPath(我怀疑他们支持)。在那种情况下,你可以简单地做

    urls = tree.xpath('//div[@class="pics"]/a/@href') + 
           tree.xpath('//div[@class="name"]/a/@href')
    

    【讨论】:

    • 这太好了,我什至不知道 xpath。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-22
    • 1970-01-01
    • 2015-01-20
    • 2011-06-29
    • 1970-01-01
    • 2012-09-21
    • 1970-01-01
    相关资源
    最近更新 更多