2014-07-15 22 views
8

ile yanıt veriyor Ayaklarımı BS ile ıslatmaya çalışıyorum. Ben zaten bir sorunla karşılaştığım ilk adımda belgeleme butat üzerinden çalışmayı denedim. ** Ben http aramak için denemek çünküBeautifulSoup,

Warning (from warnings module): 
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/bs4/__init__.py", line 189 
'"%s" looks like a URL. Beautiful Soup is not an HTTP client. You should probably use an  
HTTP client to get the document behind the URL, and feed that document to Beautiful Soup.' % markup) 
UserWarning: "https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=5...b&per_page=250&accuracy=1&has_geo=1&extras=geo,tags,views,description" 
looks like a URL. Beautiful Soup is not an HTTP client. You should 
probably use an HTTP client to get the document behind the URL, and feed that document  
to Beautiful Soup. 
https://api.flickr.com/services/rest/?method=flickr.photos.search&api;_key=5...b&per;_page=250&accuracy;=1&has;_geo=1&extras;=geo,tags,views,description 

o ** s mi yoksa başka bir sorundur:

from bs4 import BeautifulSoup 
soup = BeautifulSoup('https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=5....1b&per_page=250&accuracy=1&has_geo=1&extras=geo,tags,views,description') 

print(soup.prettify()) 

Bu cevap alıyorum edilir:

Bu benim kodudur? Yardımlarınız için teşekkürler!

+0

kaydet web sayfası urlib2 kullanmanızı dosya üzerinde çorba kullanabilirsiniz içeriği indirmek için. – suspectus

cevap

10

URL'yi bir dize olarak geçiriyorsunuz. urlopen(), sen urlopen() sonucuna read() aramak gerekmez

from urllib2 import urlopen # for Python 3: from urllib.request import urlopen 
from bs4 import BeautifulSoup 

URL = 'https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=5....1b&per_page=250&accuracy=1&has_geo=1&extras=geo,tags,views,description' 
soup = BeautifulSoup(urlopen(URL)) 

Not BeautifulSoup İlk argüman dosya benzeri bir nesne olmasını sağlar: Bunun yerine urllib2 veya requests aracılığıyla Sayfa kaynağını almak gerekir dosya benzeri bir nesne döndürür.

2

Hata herşeyi söylüyor, bir URL'yi Güzel Çorba'ya iletiyorsunuz. İlk önce web sitesi içeriğini almanız ve içeriği daha sonra BS'ye iletmeniz gerekir.

sonra lokal olarak

import urllib2 
response = urllib2.urlopen('http://www.example.com/') 
html = response.read() 

ve üstü

soup = BeautifulSoup(html) 
+0

Hey, ilk önce hangi cevabın yayınlandığını anlamaya çalıştım ve her ikisi de "29 dakika önce cevapladı" dedi. Ben de bir tanesini kabul ettim ve diğerini kabul ettim. Bunun nasıl doğru olduğundan emin değildim. Önce gelen cevabı kabul etmek istedim. – Stophface