웹 페이지에서 이미지를 가져 오는 Python 스크립트를 편집하고 있습니다 (개인 로그인이 필요하므로 링크를 게시하는 지점이 없습니다). BeautifulSoup 라이브러리를 사용하고 원본 스크립트는 here입니다. 내가하고 싶은 무엇웹 페이지에서 이미지를 가져 오는 방법
는 id 속성 id="fimage"
이있는 HTML 태그있는 하나의 이미지를 얻기 위해이 스크립트를 사용자 정의 할 수 있습니다. 수업이 없습니다. 코드는 다음과 같습니다.
from bs4 import BeautifulSoup
import urllib.request
import urllib.parse
import urllib.error
from urllib.request import urlopen
# use this image scraper from the location that
#you want to save scraped images to
def make_soup(url):
html = urlopen(url).read()
return BeautifulSoup(html)
def get_images(url):
soup = make_soup(url)
#this makes a list of bs4 element tags
images = [img for img in soup.find(id="fimage")]
print (images)
print (str(len(images)) + " images found.")
# print 'Downloading images to current working directory.'
#compile our unicode list of image links
image_links = [each.get('src') for each in images]
for each in image_links:
filename=each.split('/')[-1]
urlretrieve(each, filename)
return image_links
get_images('http://myurl');
#a standard call looks like this
#get_images('http://www.wookmark.com')
무엇인가의 이유로이 기능이 작동하지 않는 것 같습니다. 명령 줄에서 실행하면 출력을 생성합니다
[]
0 images found.
UPDATE :
좋아 그래서 난 코드를 변경했으며 이제 스크립트을 다운로드하기 위해 노력하고있어 이미지를 찾을 것으로 보인다, 실행하면 다른 오류가 발생하여 다운로드 할 수 없습니다. 명령 행에 출력을 실행
from bs4 import BeautifulSoup
from urllib import request
import urllib.parse
import urllib.error
from urllib.request import urlopen
def make_soup(url):
html = urlopen(url).read()
return BeautifulSoup(html)
def get_images(url):
soup = make_soup(url)
#this makes a list of bs4 element tags
image = soup.find(id="logo", src=True)
if image is None:
print('No images found.')
return
image_link = image['src']
filename = image_link.split('/')[-1]
request.urlretrieve(filename)
return image_link
try:
get_images('https://pypi.python.org/pypi/ClientForm/0.2.10');
except ValueError as e:
print("File could not be retrieved.", e)
else:
print("It worked!")
#a standard call looks like this
#get_images('http://www.wookmark.com')
은 다음과 같습니다 :
File could not be retrieved. unknown url type: 'python-logo.png'
.find를 .find_all로 변경하면이 목록이 반환됩니까? 아니면 하나의 이미지로 페이지를 검색하지만 프로그래밍 방식으로 둘 이상의 이미지를 검색하는 문제입니까? –
@DrBrown : 예,'.find_all()'은 하나의 결과 (첫 번째) 인'.find()'리스트를 반환합니다. 문제는 당신이 그 하나의 결과를리스트로 다루고 있으며, BeautifulSoup 엘리먼트가 반복 될 수 있다는 것입니다. 그래서'for img in image_object' *는 작동합니다, 그러나 그것은 '' 태그의 자식 요소를 나열 할 것이고 결코 존재하지 않을 것입니다. –
아직 결과가 없습니다. 이미지를보기 위해 페이지에 로그인해야한다는 사실 때문에 스크립트의 스크립트 기능에 영향을 줍니까? –