如何将BeautifulSoup和lxml一起使用?

时间:2012-11-20 15:55:51

标签: python html-parsing beautifulsoup lxml

我是Python的新手。如何一起使用BeautifulSoup和lxml?

建议在beautifulsoup website

中使用lxml作为解析器
def get_html():

        from bs4 import BeautifulSoup
        import lxml

        soup = BeautifulSoup(open("http://www.google.com"));
        #print(soup.prettify());
        print(soup.title);

if __name__ == '__main__':
        get_html()

1 个答案:

答案 0 :(得分:2)

在调用BeautifulSoup()构造函数时指定解析器:

import urllib2
from bs4 import BeautifulSoup

soup = BeautifulSoup(urllib2.urlopen("http://www.google.com").read(), "lxml")