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

2024-04-28 11:17:53 发布

您现在位置:Python中文网/ 问答频道 /正文

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

import urllib                                       
sock = urllib.urlopen("http://SOMEWHERE") 
htmlSource = sock.read()                            
sock.close()                                        
--> write to file

有没有办法在不从urllib生成文件的情况下调用BeautifulSoup?


Tags: 文件toimporthttpclosereadhtml页面
1条回答
网友
1楼 · 发布于 2024-04-28 11:17:53
from BeautifulSoup import BeautifulSoup

soup = BeautifulSoup(htmlSource)

无需编写文件:只需传入HTML字符串。还可以直接传递从urlopen返回的对象:

f = urllib.urlopen("http://SOMEWHERE") 
soup = BeautifulSoup(f)

相关问题 更多 >