簡體   English   中英

"soup.prettify()" 只給出 URL

[英]"soup.prettify()" gives just URL

我正在使用 Python3,BeautifulSoup4

當我運行下面的代碼時,它只提供網址“www.google.com”而不是 XML。 我找不到它有什么問題。

from bs4 import BeautifulSoup
import urllib


html = "www.google.com"

soup = BeautifulSoup(html)


print (soup.prettify())

您需要使用 urllib2 或類似的庫來獲取 HTML

import urllib2
html = urllib2.urlopen("www.google.com")

soup = BeautifulSoup(html)

print (soup.prettify())

編輯:作為一個旁注來澄清我為什么建議 urllib2。 如果您閱讀 urllib 文檔,您會發現“Python 3 中的 urlopen() 函數已被 urllib2.urlopen() 刪除”。 鑒於您已標記 Python3,urllib2 可能是您的最佳選擇。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM