2012-07-01 83 views
0

我想打印第一輪起草的30個NBA球隊的圖像。但是,當我告訴它打印它打印出的鏈接,而不是圖像。如何讓它打印出圖像而不是給我圖像鏈接。 這裏是我的代碼:如何打印圖像?

import urllib2 
from BeautifulSoup import BeautifulSoup 
# or if your're using BeautifulSoup4: 
# from bs4 import BeautifulSoup 

soup = BeautifulSoup(urllib2.urlopen('http://www.cbssports.com/nba/draft/mock-draft').read()) 

rows = soup.findAll("table", attrs = {'class': 'data borderTop'})[0].tbody.findAll("tr")[2:] 

for row in rows: 
    fields = row.findAll("td") 
    if len(fields) >= 3: 
    anchor = row.findAll("td")[1].find("a") 
    if anchor: 
     print anchor 
+2

如何打印圖像?你是什​​麼意思「打印圖像」? –

+0

瀏覽器根據HTML源代碼中給出的鏈接加載圖像,並與圖像所在的服務器建立新的連接;你將需要做同樣的事情。 –

+0

好吧,如果你現在運行代碼,它會給我圖像鏈接。我想讓代碼自己顯示圖片而不是圖片鏈接。 – user1470901

回答

0

您需要將文件轉換成64位編碼,這會給數據的字符串,可以到處移動,如果需要存儲。我通常用PHP做這件事,所以如果Python有點粗糙,我很抱歉

import urllib2 
import base64 
url = 'http://somesite.com/images/team_one.jpg' 
grab = urllib2.urlopen(url) 
encoded_image_string = base64.b64encode(grab.read())