Python 美如素无img

Python 美如素无img,python,html,python-2.7,beautifulsoup,html-parsing,Python,Html,Python 2.7,Beautifulsoup,Html Parsing,我正试图使用bs4在Python2.7中编写一个脚本,以刮取图像并将文件重命名到我的服务器上,以低带宽友好的方式显示,并通过覆盖现有图像每隔3小时在cronjobs上更新一次 我的代码中的问题是什么都没有出现,甚至没有错误 以下是实际代码: import requests import random from bs4 import BeautifulSoup def download_web_image(url): name = random.randrange(1, 1000)

我正试图使用bs4在Python2.7中编写一个脚本,以刮取图像并将文件重命名到我的服务器上,以低带宽友好的方式显示,并通过覆盖现有图像每隔3小时在cronjobs上更新一次

我的代码中的问题是什么都没有出现,甚至没有错误

以下是实际代码:

import requests
import random
from bs4 import BeautifulSoup

def download_web_image(url):
    name = random.randrange(1, 1000)
    full_name = str(name) + "psdata.gif"
    urllib.request.urlretrieve(url, full_name)

timecapture = (0, 24, 48, 72)

for time in timecapture:
    url = 'http://www.weatheronline.co.uk/marine/weather?LEVEL=4&LANG=en&TIME=' + str(time) + '&CEL=C&SI=mph&MN=gfs&MODELLTYP=pslv&WIND=g205'
    source_code = requests.get(url)
    plain_text = source_code.text
    soup = BeautifulSoup(plain_text)
    for link in soup.find('img', src=True):
        href = 'http://www.weatheronline.co.uk' + link.get('href')
        download_web_image(href)
网页中的数据位于此标记之间:

<div class="zent">
    <img usemap="#karte" class="eMap" id="pictureid" src="/daten/sailcharts/gfs/2015/03/11/pslv_poly_06-2015031018.gif" border="0" alt="We 11.03.2015 06 UTC" width="634" height="490">
</div>

通过
id
获取图片。要加入URL部分,请使用:


下面将为您提供指向任何带有“src”的“img”的链接:

samplehtml="""
<div class="zent">
    <img usemap="#karte" class="eMap" id="pictureid" src="/daten/sailcharts/gfs/2015/03/11/pslv_poly_06-2015031018.gif" border="0" alt="We 11.03.2015 06 UTC" width="634" height="490">
    <img usemap="#karte" class="eMap" id="pictureid" src="/daten/sailcharts/gfs/2015/03/11/pslv_poly_06-2015031018.gif" border="0" alt="We 11.03.2015 06 UTC" width="634" height="490">
</div>
"""

baseurl = 'http://www.weatheronline.co.uk'

from bs4 import BeautifulSoup
imglinks = [baseurl+x['src'] for x in BeautifulSoup(samplehtml).find_all('img',src=True)]
print imglinks
尝试:
[link.get('href')获取汤中的链接。选择('#pictureid')]
samplehtml="""
<div class="zent">
    <img usemap="#karte" class="eMap" id="pictureid" src="/daten/sailcharts/gfs/2015/03/11/pslv_poly_06-2015031018.gif" border="0" alt="We 11.03.2015 06 UTC" width="634" height="490">
    <img usemap="#karte" class="eMap" id="pictureid" src="/daten/sailcharts/gfs/2015/03/11/pslv_poly_06-2015031018.gif" border="0" alt="We 11.03.2015 06 UTC" width="634" height="490">
</div>
"""

baseurl = 'http://www.weatheronline.co.uk'

from bs4 import BeautifulSoup
imglinks = [baseurl+x['src'] for x in BeautifulSoup(samplehtml).find_all('img',src=True)]
print imglinks
[u'http://www.weatheronline.co.uk/daten/sailcharts/gfs/2015/03/11/pslv_poly_06-2015031018.gif',
u'http://www.weatheronline.co.uk/daten/sailcharts/gfs/2015/03/11/pslv_poly_06-2015031018.gif']