Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用urllib和BeautifulSoup使用Python从web检索信息_Python_Web Scraping_Beautifulsoup_Urllib2 - Fatal编程技术网

使用urllib和BeautifulSoup使用Python从web检索信息

使用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()

我可以使用urllib获取html页面,并使用BeautifulSoup解析html页面,看起来我必须生成要从BeautifulSoup读取的文件

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/")