使用urllib和BeautifulSoup使用Python从web检索信息
我可以使用urllib获取html页面,并使用BeautifulSoup解析html页面,看起来我必须生成要从BeautifulSoup读取的文件使用urllib和BeautifulSoup使用Python从web检索信息,python,web-scraping,beautifulsoup,urllib2,Python,Web Scraping,Beautifulsoup,Urllib2,我可以使用urllib获取html页面,并使用BeautifulSoup解析html页面,看起来我必须生成要从BeautifulSoup读取的文件 import urllib sock = urllib.urlopen("http://SOMEWHERE") htmlSource = sock.read() sock.close()
import urllib
sock = urllib.urlopen("http://SOMEWHERE")
htmlSource = sock.read()
sock.close()
--> write to file
有没有一种方法可以在不从urllib生成文件的情况下调用BeautifulSoup
from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(htmlSource)
无需编写文件:只需传入HTML字符串。您还可以直接传递从urlopen
返回的对象:
f = urllib.urlopen("http://SOMEWHERE")
soup = BeautifulSoup(f)
您可以打开url,下载html,并使用以下工具一次性解析: 来自gazpacho进口汤的
汤https://www.example.com/")