我们可以将xpath与BeautifulSoup一起使用吗?

发布于 2021-01-29 19:30:58

我正在使用BeautifulSoup抓取网址,并且我有以下代码

import urllib
import urllib2
from BeautifulSoup import BeautifulSoup

url =  "http://www.example.com/servlet/av/ResultTemplate=AVResult.html"
req = urllib2.Request(url)
response = urllib2.urlopen(req)
the_page = response.read()
soup = BeautifulSoup(the_page)
soup.findAll('td',attrs={'class':'empformbody'})

现在在上面的代码中,我们可以findAll用来获取标签和与其相关的信息,但是我想使用xpath。是否可以将xpath与BeautifulSoup一起使用?如果可能的话,任何人都可以给我提供示例代码,以使其更有帮助吗?

关注者
0
被浏览
163
1 个回答
  • 面试哥
    面试哥 2021-01-30
    为面试而生,有面试问题,就找面试哥。

    不,BeautifulSoup本身不支持XPath表达式。

    另一种库,LXML 支持的XPath
    1.0。它具有BeautifulSoup兼容模式,它将以Soup的方式尝试解析损坏的HTML。但是,默认的lxml
    HTML解析器
    可以很好地完成解析损坏的HTML的工作,而且我相信它的速度更快。

    将文档解析为lxml树后,就可以使用该.xpath()方法搜索元素。

    try:
        # Python 2
        from urllib2 import urlopen
    except ImportError:
        from urllib.request import urlopen
    from lxml import etree
    
    url =  "http://www.example.com/servlet/av/ResultTemplate=AVResult.html"
    response = urlopen(url)
    htmlparser = etree.HTMLParser()
    tree = etree.parse(response, htmlparser)
    tree.xpath(xpathselector)
    

    还有一个带有附加功能的专用lxml.html()模块

    请注意,在上面的示例中,我将response对象直接传递给lxml,因为直接从流中读取解析器比将响应首先读取到大字符串中更有效。要对requests库执行相同的操作,您需要在启用透明传输解压缩后设置stream=True并传递response.raw对象:)

    import lxml.html
    import requests
    
    url =  "http://www.example.com/servlet/av/ResultTemplate=AVResult.html"
    response = requests.get(url, stream=True)
    response.raw.decode_content = True
    tree = lxml.html.parse(response.raw)
    

    您可能会感兴趣的是CSS选择器支持;在CSSSelector类转换CSS语句转换为XPath表达式,使您的搜索td.empformbody更加容易:

    from lxml.cssselect import CSSSelector
    
    td_empformbody = CSSSelector('td.empformbody')
    for elem in td_empformbody(tree):
        # Do something with these table cells.
    

    即将来临:BeautifulSoup本身 确实
    具有非常完整的CSS选择器支持

    for cell in soup.select('table#foobar td.empformbody'):
        # Do something with these table cells.
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看