【问题标题】:Screen scraping in LXML with python-- extract specific data使用python在LXML中进行屏幕抓取——提取特定数据
【发布时间】:2011-06-10 06:22:54
【问题描述】:

在过去的几个小时里,我一直在尝试编写一个程序来完成我认为非常简单的任务:

  1. 程序要求用户输入(假设类型为“幸福”)
  2. 程序使用这种格式查询 thinkexist 网站(“http://thinkexist.com/search/searchQuotation.asp?search=USERINPUT”)
  3. 程序从网站返回第一个报价。

我尝试过将 Xpath 与 lxml 一起使用,但没有经验,并且每个构造都返回一个空白数组。

引用的实际内容似乎包含在“sqq”类中。

如果我通过 Firebug 浏览该站点,单击 DOM 选项卡,它似乎引用在 textNode 属性“wholeText”或“textContent”中——但我不知道如何以编程方式使用该知识。

有什么想法吗?

【问题讨论】:

    标签: python screen-scraping screen lxml web-scraping


    【解决方案1】:

    如果你不需要通过 XPath 来实现,你可以像这样使用BeautifilSoup 库(让myXml 变量包含页面 HTML 源代码):

    soup = BeautifulSoup(myXml)
    for a in soup.findAll(a,{'class' : 'sqq'}):
      # this is your quote
      print a.contents
    

    无论如何,请阅读 BS 文档,它对于一些不需要 XPath 功能的抓取需求可能非常有用。

    【讨论】:

    • 光荣!仅针对需要帮助的其他人:for 行应该写成:for a in soup.findAll('a',{'class' : 'sqq'}, limit = 1)
    • 问题:如何将列表转换为没有奇怪的 标签和“[u']”符号的字符串?例如,你的搜索狗的程序会产生这样的结果:[你很幸运,对我们来说,这个问题很久以前就被因纽特人的猎人解决了。他们发现他们的',,你'可以闻到雪下的密封洞。']
    • 你所谓的“[u']”符号只是比字符串更宽的Unicode对象。如果你想正确地抓取一些非 ASCI 符号,你应该使用 Unicode 对象。换句话说,如果你确定简单的字符串对你来说已经足够了,那么从 Unicode 对象到字符串的转换就是这样完成的:str(u'Hello, I am Unicode')。但小心点!如果您尝试转换包含一些(例如中文符号)的 Unicode 对象,您将收到错误消息。
    • 第一个问题的答案很简单:您可能总是会自己编写一点代码。 BuautifulSoup.Tag.countents 会返回一个内容列表,存储在当前标签中。如果您只想获取纯文本,则可以使用类似这样的代码和平: def raw_text(s): if isinstance(s, str): return unicode(s); if isinstance(s, unicode): return s; if isinstance(s, list): return ''.join([raw_text(si) for si in s]); if isinstance(s, BeautifulSoup): return raw_text(s.contents); if isinstance(s, Tag): return raw_text(s.contents);返回 s.__unicode__()
    【解决方案2】:
    import lxml.html
    import urllib
    
    site = 'http://thinkexist.com/search/searchquotation.asp'
    
    userInput = raw_input('Search for: ').strip()
    url = site + '?' + urllib.urlencode({'search':userInput})
    
    root = lxml.html.parse(url).getroot()
    quotes = root.xpath('//a[@class="sqq"]')
    
    print quotes[0].text_content()
    

    ...如果您输入“莎士比亚”,它会返回

    In real life, unlike in Shakespeare, the sweetness
    of the rose depends upon the name it bears.  Things
    are not only what they are.  They are, in very important
    respects, what they seem to be.
    

    【讨论】:

    • 哇。太棒了。我一直在尝试使用 Beautiful Soup,但是每当它返回“sqq”类的内容时,它都包含一个 标签,我无法删除它。非常感谢。
    【解决方案3】:

    您可以打开 html 源代码来找出您要查找的确切类。例如,要获取页面上遇到的第一个 StackOverflow 用户名,您可以这样做:

    #!/usr/bin/env python
    from lxml import html
    
    url = 'http://stackoverflow.com/questions/4710307'
    tree = html.parse(url)
    path = '//div[@class="user-details"]/a[@href]'
    print tree.findtext(path)
    # -> Parseltongue
    # OR to print text including the text in children
    a = tree.find(path)
    print a.text_content()
    # -> Parseltongue
    

    【讨论】:

    • 非常感谢您的帮助。我想我的 xpath 语法不正确。
    猜你喜欢
    • 2010-10-23
    • 2011-01-12
    • 1970-01-01
    • 1970-01-01
    • 2013-12-31
    • 2013-01-12
    • 2017-02-25
    • 2023-03-25
    • 1970-01-01
    相关资源
    最近更新 更多