我正在尝试使用bs4在python 2.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部分,请使用urlparse.urljoin()
:
base_url = 'http://www.weatheronline.co.uk'
print urljoin(base_url, soup.find('img', id='pictureid')['src'])
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句