Python爬虫:如何创建BeautifulSoup对象

来源:互联网 发布:淘宝买彩票在哪里 编辑:程序博客网 时间:2024/05/18 00:04
from urllib.request import urlopenfrom bs4 import BeautifulSouphtml = '<div>text1</div>'html = urlopen("http://www.pythonscraping.com/pages/page3.html")html = open('c:\\aa.html')#以上三行表示了HTML的三种来源,一是字符串形式,二是在线网页形式,三是HTML文件形式bsObj = BeautifulSoup(html, 'html.parser') # 'html.parser'是解析器,也可以用'lxml'# BeautifulSoup类似于C++中的构造函数e = bsObj.find('div')print(e.text)