Python下载带有变化变量的图像

时间:2016-07-21 14:04:18

标签: python image url urllib python-3.5

我想从其中包含import urllib.request import random random_number=random.randint(500,600) url_image="'https://csgostash.com/img/skins/s"+str(random_number)+"fn.png'" image=urllib.request.urlretrieve(url_image, 'skin.png') 的网址链接下载图片,因此我生成了相同的代码,但我收到了错误 -

代码:

Traceback (most recent call last):
  File "C:/Users/luke/Desktop/scraper/test image download/cs test.py", line 8, in <module>
    image=urllib.request.urlretrieve(url_image, 'skin.png')
  File "C:\Users\luke\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 187, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
  File "C:\Users\luke\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 162, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\luke\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 465, in open
    response = self._open(req, data)
  File "C:\Users\luke\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 488, in _open
    'unknown_open', req)
  File "C:\Users\luke\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 443, in _call_chain
    result = func(*args)
  File "C:\Users\luke\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 1310, in unknown_open
    raise URLError('unknown url type: %s' % type)
urllib.error.URLError: <urlopen error unknown url type: 'https>

错误:

    <nav class="navbar navbar-default navbar-fixed-top">
    <div class="container">
        <div class="navbar-header">        
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#myPage"><span class="glyphicon glyphicon-home logo-inverse"></span></a>
            <p class="navbar-text">Put you alert HERE with class navbar-text</p>
        </div>
        <div class="collapse navbar-collapse" id="myNavbar">
            <ul class="nav navbar-nav navbar-right text-uppercase">
                <li><a href="#about" data-toggle="collapse" data-target=".navbar-collapse.in">about</a></li>
                <!--<li><a href="#portfolio">PORTFOLIO</a></li>-->
                <li><a href="#pricing" data-toggle="collapse" data-target=".navbar-collapse.in">products</a></li>
                <!--<li class="dropdown">
                    <a class="dropbtn" data-toggle="dropdown"  href="">
                        products
                        <span class="caret"></span>
                    </a>
                    <ul style="list-style:none !important; padding:0px 0px !important;" class="dropdown-content">
                        <li><a href="#CPR_WEB" data-toggle="collapse" data-target=".navbar-collapse.in">CPR-Web</a></li>
                        <li><a href="#SC_TRACKER" data-toggle="collapse" data-target=".navbar-collapse.in">SC-Tracker</a></li>
                        <li><a href="#" data-toggle="collapse" data-target=".navbar-collapse.in">Page 1-3</a></li>
                    </ul>
                </li>-->
                <li><a href="#contact" data-toggle="collapse" data-target=".navbar-collapse.in">contact</a></li>
                <li><a href="#recentnews" data-toggle="collapse" data-target=".navbar-collapse.in">recent news</a></li>
            </ul>
        </div>
    </div>
</nav>

2 个答案:

答案 0 :(得分:0)

首先,url_image有一种奇怪的语法。

refresh fast

如果你解决了这个问题,你会发现403-Vax!防范僵尸:使用用户代理。

    url_image="https://csgostash.com/img/skins/s"+str(random_number)+"fn.png"

编辑:当然你可以保存到文件:

import urllib.request
import random

random_number=random.randint(500,600)
url_image="https://csgostash.com/img/skins/s"+str(random_number)+"fn.png"

user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)'
headers = {'User-Agent': user_agent}
req = urllib.request.Request(url_image, None, headers)


print(url_image)
#image, h = urllib.request.urlretrieve(url_image)
with urllib.request.urlopen(req) as response:
    the_page = response.read()

print (the_page)

答案 1 :(得分:0)

使用请求检出this project

enter image description here