Python 使用BeautifulSoup从有序列表中刮取图像HREF

Python 使用BeautifulSoup从有序列表中刮取图像HREF,python,html,beautifulsoup,Python,Html,Beautifulsoup,我正在尝试从该网站检索图像(经许可)。以下是我想要访问的网站的代码: import urllib2 from bs4 import BeautifulSoup url = "http://www.vgmuseum.com/nes.htm" page = urllib2.urlopen(url).read() soup = BeautifulSoup(page, "html5lib") li = soup.select('ol > li > a') for link in li:

我正在尝试从该网站检索图像(经许可)。以下是我想要访问的网站的代码:

import urllib2
from bs4 import BeautifulSoup

url = "http://www.vgmuseum.com/nes.htm"

page = urllib2.urlopen(url).read()
soup = BeautifulSoup(page, "html5lib")
li = soup.select('ol > li > a')
for link in li:
   print(link.get('href'))
我想使用的图像位于以下有序列表中:

您正在使用的页面包含
iframe
s,这基本上是将一个页面包含到另一个页面中的一种方式。浏览器了解iframe的工作方式,可以下载页面并在浏览器窗口中显示

然而,urllib2不是浏览器,不能这样做。您需要探索链接列表的位置,即
iframe
所在的位置,然后按照此
iframe
内容的url进行操作。在您的例子中,左边的链接列表来自
http://www.vgmuseum.com/nes_b.html
第页

下面是一个可行的解决方案,可以跟踪链接列表中的链接,下载包含图像的页面,并将其下载到
images/
目录中。我正在使用
requests
模块,并利用
lxml
解析器与
BeautifulSoup
合作,以更快地解析HTML:

from urllib.parse import urljoin

import os
import requests
from bs4 import BeautifulSoup

url = "http://www.vgmuseum.com/nes_b.html"


def download_image(session, url):
    print(url)
    local_filename = os.path.join("images", url.split('/')[-1])

    r = session.get(url, stream=True)
    with open(local_filename, 'wb') as f:
        for chunk in r.iter_content(chunk_size=1024):
            if chunk:  # filter out keep-alive new chunks
                f.write(chunk)


with requests.Session() as session:
    session.headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'
    }
    response = session.get(url)
    soup = BeautifulSoup(response.content, "lxml")

    for link in soup.select('ol > li > a[href*=images]'):
        response = session.get(urljoin(response.url, link.get('href')))
        for image in BeautifulSoup(response.content, "lxml").select("img[src]"):
            download_image(session, url=urljoin(response.url, image["src"]))

我使用上面@Dan评论中的url进行解析

代码:

输出:

images/nes/10yard.html
images/nes2/10.html
pics2/100man.html
images/nes/1942.html
images/nes2/1942.html
images/nes/1943.html
images/nes2/1943.html
pics7/1944.html
images/nes/1999.html
images/nes2/2600.html
images/nes2/3dbattles.html
images/nes2/3dblock.html
images/nes2/3in1.html
images/nes/4cardgames.html
pics2/4.html
images/nes/4wheeldrivebattle.html
images/nes/634.html
images/nes/720NES.html
images/nes/8eyes.html
images/nes2/8eyes.html
images/nes2/8eyesp.html
pics2/89.html
images/nes/01/blob.html
pics5/boy.html
images/03/a.html
images/03/aa.html
images/nes/abadox.html
images/03/abadoxf.html
images/03/abadoxj.html
images/03/abadoxp.html
images/03/abarenbou.html
images/03/aces.html
images/03/action52.html
images/03/actionin.html
images/03/adddragons.html
images/03/addheroes.html
images/03/addhillsfar.html
images/03/addpool.html
pics/addamsfamily.html
pics/addamsfamilypugsley.html
images/nes/01/adventureislandNES.html
images/nes/adventureisland2.html
images/nes/advisland3.html
pics/adventureisland4.html
images/03/ai4.html
images/nes/magickingdom.html
pics/bayou.html
images/03/bayou.html
images/03/captain.html
images/nes/adventuresofdinoriki.html
images/03/ice.html
images/nes/01/lolo1.html
images/03/lolo.html
images/nes/01/adventuresoflolo2.html
images/03/lolo2.html
images/nes/adventuresoflolo3.html
pics/radgravity.html
images/03/rad.html
images/nes/01/rockyandbullwinkle.html
images/nes/01/tomsawyer.html
images/03/afroman.html
images/03/afromario.html
pics/afterburner.html
pics2/afterburner2.html
images/03/ai.html
images/03/aigiina.html
images/nes/01/airfortress.html
images/03/air.html
images/03/airk.html
images/nes/01/airwolf.html
images/03/airwolfe.html
images/03/airwolfj.html
images/03/akagawa.html
images/nes/01/akira.html
images/03/akka.html
images/03/akuma.html
pics2/adensetsu.html
pics2/adracula.html
images/nes/01/akumajo.html
pics2/aspecial.html
pics/alunser.html
images/nes/01/alfred.html
images/03/alice.html
images/nes/01/alien3.html
images/nes/01/asyndrome.html
images/03/alien.html
images/03/all.html
images/nes/01/allpro.html
images/nes/01/allstarsoftball.html
images/nes/01/alphamission.html
pics2/altered.html

您首先必须刮除左边框(
http://www.vgmuseum.com/nes_b.html
)要获得与所需的每组图像对应的URL列表,则需要转到这些URL并刮取图像,因此这是一个多步骤的过程。我不会为您编写它,因为您有一个良好的开端,但这就是您的做法。我不会要求任何人为我编写代码。我只是问有什么需要改进的。这是我第一次使用这个图书馆。谢谢你的回答
images/nes/10yard.html
images/nes2/10.html
pics2/100man.html
images/nes/1942.html
images/nes2/1942.html
images/nes/1943.html
images/nes2/1943.html
pics7/1944.html
images/nes/1999.html
images/nes2/2600.html
images/nes2/3dbattles.html
images/nes2/3dblock.html
images/nes2/3in1.html
images/nes/4cardgames.html
pics2/4.html
images/nes/4wheeldrivebattle.html
images/nes/634.html
images/nes/720NES.html
images/nes/8eyes.html
images/nes2/8eyes.html
images/nes2/8eyesp.html
pics2/89.html
images/nes/01/blob.html
pics5/boy.html
images/03/a.html
images/03/aa.html
images/nes/abadox.html
images/03/abadoxf.html
images/03/abadoxj.html
images/03/abadoxp.html
images/03/abarenbou.html
images/03/aces.html
images/03/action52.html
images/03/actionin.html
images/03/adddragons.html
images/03/addheroes.html
images/03/addhillsfar.html
images/03/addpool.html
pics/addamsfamily.html
pics/addamsfamilypugsley.html
images/nes/01/adventureislandNES.html
images/nes/adventureisland2.html
images/nes/advisland3.html
pics/adventureisland4.html
images/03/ai4.html
images/nes/magickingdom.html
pics/bayou.html
images/03/bayou.html
images/03/captain.html
images/nes/adventuresofdinoriki.html
images/03/ice.html
images/nes/01/lolo1.html
images/03/lolo.html
images/nes/01/adventuresoflolo2.html
images/03/lolo2.html
images/nes/adventuresoflolo3.html
pics/radgravity.html
images/03/rad.html
images/nes/01/rockyandbullwinkle.html
images/nes/01/tomsawyer.html
images/03/afroman.html
images/03/afromario.html
pics/afterburner.html
pics2/afterburner2.html
images/03/ai.html
images/03/aigiina.html
images/nes/01/airfortress.html
images/03/air.html
images/03/airk.html
images/nes/01/airwolf.html
images/03/airwolfe.html
images/03/airwolfj.html
images/03/akagawa.html
images/nes/01/akira.html
images/03/akka.html
images/03/akuma.html
pics2/adensetsu.html
pics2/adracula.html
images/nes/01/akumajo.html
pics2/aspecial.html
pics/alunser.html
images/nes/01/alfred.html
images/03/alice.html
images/nes/01/alien3.html
images/nes/01/asyndrome.html
images/03/alien.html
images/03/all.html
images/nes/01/allpro.html
images/nes/01/allstarsoftball.html
images/nes/01/alphamission.html
pics2/altered.html