![](/img/trans.png)
[英]Trying to retrieve data from a template in a website using urllib2 and BeautifulSoup
[英]Using urllib and BeautifulSoup to retrieve info from web with Python
我可以使用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一次性解析它:
from gazpacho import Soup
soup = Soup.get("https://www.example.com/")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.