質問編集履歴
8
コード追加
    
        title	
    CHANGED
    
    | 
            File without changes
         | 
    
        body	
    CHANGED
    
    | @@ -47,4 +47,5 @@ | |
| 47 47 | 
             
                  6     data = json.loads(r.text)
         | 
| 48 48 |  | 
| 49 49 | 
             
            KeyError: 'MMM'
         | 
| 50 | 
            +
             | 
| 50 51 | 
             
            ```
         | 
7
文法修正
    
        title	
    CHANGED
    
    | 
            File without changes
         | 
    
        body	
    CHANGED
    
    | @@ -1,10 +1,28 @@ | |
| 1 1 | 
             
            以下のコードを実行したのですが、エラーが出てしまいます。
         | 
| 2 2 | 
             
            ご指摘、ご教授お願いします。
         | 
| 3 | 
            -
             | 
| 3 | 
            +
             | 
| 4 | 
            -
            [スクレイピング先のLink](https://home.openweathermap.org/api_keys)
         | 
| 5 4 | 
             
            ```python
         | 
| 5 | 
            +
            import requests
         | 
| 6 | 
            +
            import json
         | 
| 7 | 
            +
            ↓
         | 
| 8 | 
            +
            apikey = "MMM"
         | 
| 9 | 
            +
            ↓
         | 
| 10 | 
            +
            cities = ["London,UK", "Tokyo,JP", "New York,US"]
         | 
| 11 | 
            +
            ↓
         | 
| 12 | 
            +
            api = "http://api.openweathermap.org/data/2.5/forecast?q=Tokyo&APPID={MMM}"
         | 
| 13 | 
            +
            ↓
         | 
| 14 | 
            +
            k2c =lambda k: k - 273.15
         | 
| 15 | 
            +
            ↓
         | 
| 6 16 | 
             
            for name in cities:
         | 
| 7 17 | 
             
                print("London,UK", "Tokyo,JP", "New York,US")
         | 
| 18 | 
            +
            ↓
         | 
| 19 | 
            +
            for name in cities:
         | 
| 20 | 
            +
                print("London,UK", "Tokyo,JP", "New York,US")
         | 
| 21 | 
            +
            ```
         | 
| 22 | 
            +
            エラー内容。
         | 
| 23 | 
            +
            ```python
         | 
| 24 | 
            +
            for name in cities:
         | 
| 25 | 
            +
                print("London,UK", "Tokyo,JP", "New York,US")
         | 
| 8 26 |  | 
| 9 27 | 
             
                url = api.format(city=name, key=apikey)
         | 
| 10 28 | 
             
                r = requests.get(url)
         | 
| @@ -17,110 +35,16 @@ | |
| 17 35 | 
             
                print("| 気圧 =", data["main"]["pressure"])
         | 
| 18 36 | 
             
                print("| 風速度 =", data["wind"]["speed"])
         | 
| 19 37 | 
             
                print("")
         | 
| 20 | 
            -
             | 
| 38 | 
            +
            ↓
         | 
| 21 | 
            -
            エラーコード
         | 
| 22 | 
            -
            ```python
         | 
| 23 39 | 
             
            London,UK Tokyo,JP New York,US
         | 
| 24 40 | 
             
            ---------------------------------------------------------------------------
         | 
| 25 | 
            -
             | 
| 41 | 
            +
            KeyError                                  Traceback (most recent call last)
         | 
| 26 | 
            -
            <ipython-input- | 
| 42 | 
            +
            <ipython-input-126-8a8e428ed215> in <module>
         | 
| 43 | 
            +
                  2     print("London,UK", "Tokyo,JP", "New York,US")
         | 
| 44 | 
            +
                  3 
         | 
| 27 | 
            -
             | 
| 45 | 
            +
            ----> 4     url = api.format(city=name, key=apikey)
         | 
| 28 | 
            -
                   | 
| 46 | 
            +
                  5     r = requests.get(url)
         | 
| 29 | 
            -
             | 
| 47 | 
            +
                  6     data = json.loads(r.text)
         | 
| 30 | 
            -
                  6     print("+ 都市 =", data["name"])
         | 
| 31 | 
            -
                  7     print("| 天気 =", data["weather"][0]["description"])
         | 
| 32 48 |  | 
| 33 | 
            -
            ~/.pyenv/versions/3.8.0/lib/python3.8/json/__init__.py in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
         | 
| 34 | 
            -
                355             parse_int is None and parse_float is None and
         | 
| 35 | 
            -
                356             parse_constant is None and object_pairs_hook is None and not kw):
         | 
| 36 | 
            -
            --> 357         return _default_decoder.decode(s)
         | 
| 37 | 
            -
                358     if cls is None:
         | 
| 38 | 
            -
                359         cls = JSONDecoder
         | 
| 39 | 
            -
             | 
| 40 | 
            -
            ~/.pyenv/versions/3.8.0/lib/python3.8/json/decoder.py in decode(self, s, _w)
         | 
| 41 | 
            -
                335 
         | 
| 42 | 
            -
                336         """
         | 
| 43 | 
            -
            --> 337         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
         | 
| 44 | 
            -
                338         end = _w(s, end).end()
         | 
| 45 | 
            -
                339         if end != len(s):
         | 
| 46 | 
            -
             | 
| 47 | 
            -
            ~/.pyenv/versions/3.8.0/lib/python3.8/json/decoder.py in raw_decode(self, s, idx)
         | 
| 48 | 
            -
                353             obj, end = self.scan_once(s, idx)
         | 
| 49 | 
            -
                354         except StopIteration as err:
         | 
| 50 | 
            -
            --> 355             raise JSONDecodeError("Expecting value", s, err.value) from None
         | 
| 51 | 
            -
                356         return obj, end
         | 
| 52 | 
            -
             | 
| 53 | 
            -
            JSONDecodeError: Expecting value: line 1 column 1 (char 0)
         | 
| 54 | 
            -
             | 
| 55 | 
            -
            ```
         | 
| 56 | 
            -
            ```python
         | 
| 57 | 
            -
            print(r.text)
         | 
| 58 | 
            -
            ↓
         | 
| 59 | 
            -
            <!DOCTYPE html>
         | 
| 60 | 
            -
            <html>
         | 
| 61 | 
            -
            <head>
         | 
| 62 | 
            -
              <title>The page you were looking for doesn't exist (404)</title>
         | 
| 63 | 
            -
              <meta name="viewport" content="width=device-width,initial-scale=1">
         | 
| 64 | 
            -
              <style>
         | 
| 65 | 
            -
              body {
         | 
| 66 | 
            -
                background-color: #EFEFEF;
         | 
| 67 | 
            -
                color: #2E2F30;
         | 
| 68 | 
            -
                text-align: center;
         | 
| 69 | 
            -
                font-family: arial, sans-serif;
         | 
| 70 | 
            -
                margin: 0;
         | 
| 71 | 
            -
              }
         | 
| 72 | 
            -
             | 
| 73 | 
            -
              div.dialog {
         | 
| 74 | 
            -
                width: 95%;
         | 
| 75 | 
            -
                max-width: 33em;
         | 
| 76 | 
            -
                margin: 4em auto 0;
         | 
| 77 | 
            -
              }
         | 
| 78 | 
            -
             | 
| 79 | 
            -
              div.dialog > div {
         | 
| 80 | 
            -
             | 
| 49 | 
            +
            KeyError: 'MMM'
         | 
| 81 | 
            -
                border-right-color: #999;
         | 
| 82 | 
            -
                border-left-color: #999;
         | 
| 83 | 
            -
                border-bottom-color: #BBB;
         | 
| 84 | 
            -
                border-top: #B00100 solid 4px;
         | 
| 85 | 
            -
                border-top-left-radius: 9px;
         | 
| 86 | 
            -
                border-top-right-radius: 9px;
         | 
| 87 | 
            -
                background-color: white;
         | 
| 88 | 
            -
                padding: 7px 12% 0;
         | 
| 89 | 
            -
                box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
         | 
| 90 | 
            -
              }
         | 
| 91 | 
            -
             | 
| 92 | 
            -
              h1 {
         | 
| 93 | 
            -
                font-size: 100%;
         | 
| 94 | 
            -
                color: #730E15;
         | 
| 95 | 
            -
                line-height: 1.5em;
         | 
| 96 | 
            -
              }
         | 
| 97 | 
            -
             | 
| 98 | 
            -
              div.dialog > p {
         | 
| 99 | 
            -
                margin: 0 0 1em;
         | 
| 100 | 
            -
                padding: 1em;
         | 
| 101 | 
            -
                background-color: #F7F7F7;
         | 
| 102 | 
            -
                border: 1px solid #CCC;
         | 
| 103 | 
            -
                border-right-color: #999;
         | 
| 104 | 
            -
                border-left-color: #999;
         | 
| 105 | 
            -
                border-bottom-color: #999;
         | 
| 106 | 
            -
                border-bottom-left-radius: 4px;
         | 
| 107 | 
            -
                border-bottom-right-radius: 4px;
         | 
| 108 | 
            -
                border-top-color: #DADADA;
         | 
| 109 | 
            -
                color: #666;
         | 
| 110 | 
            -
                box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
         | 
| 111 | 
            -
              }
         | 
| 112 | 
            -
              </style>
         | 
| 113 | 
            -
            </head>
         | 
| 114 | 
            -
             | 
| 115 | 
            -
            <body>
         | 
| 116 | 
            -
              <!-- This file lives in public/404.html -->
         | 
| 117 | 
            -
              <div class="dialog">
         | 
| 118 | 
            -
                <div>
         | 
| 119 | 
            -
                  <h1>The page you were looking for doesn't exist.</h1>
         | 
| 120 | 
            -
                  <p>You may have mistyped the address or the page may have moved.</p>
         | 
| 121 | 
            -
                </div>
         | 
| 122 | 
            -
                <p>If you are the application owner check the logs for more information.</p>
         | 
| 123 | 
            -
              </div>
         | 
| 124 | 
            -
            </body>
         | 
| 125 | 
            -
            </html>
         | 
| 126 50 | 
             
            ```
         | 
6
変更点追加
    
        title	
    CHANGED
    
    | 
            File without changes
         | 
    
        body	
    CHANGED
    
    | 
            File without changes
         | 
5
コード追加
    
        title	
    CHANGED
    
    | 
            File without changes
         | 
    
        body	
    CHANGED
    
    | @@ -1,5 +1,6 @@ | |
| 1 1 | 
             
            以下のコードを実行したのですが、エラーが出てしまいます。
         | 
| 2 2 | 
             
            ご指摘、ご教授お願いします。
         | 
| 3 | 
            +
            
         | 
| 3 4 | 
             
            [スクレイピング先のLink](https://home.openweathermap.org/api_keys)
         | 
| 4 5 | 
             
            ```python
         | 
| 5 6 | 
             
            for name in cities:
         | 
4
変更点追加
    
        title	
    CHANGED
    
    | 
            File without changes
         | 
    
        body	
    CHANGED
    
    | @@ -1,6 +1,6 @@ | |
| 1 1 | 
             
            以下のコードを実行したのですが、エラーが出てしまいます。
         | 
| 2 2 | 
             
            ご指摘、ご教授お願いします。
         | 
| 3 | 
            -
             | 
| 3 | 
            +
            [スクレイピング先のLink](https://home.openweathermap.org/api_keys)
         | 
| 4 4 | 
             
            ```python
         | 
| 5 5 | 
             
            for name in cities:
         | 
| 6 6 | 
             
                print("London,UK", "Tokyo,JP", "New York,US")
         | 
3
変更点追加
    
        title	
    CHANGED
    
    | 
            File without changes
         | 
    
        body	
    CHANGED
    
    | @@ -51,4 +51,75 @@ | |
| 51 51 |  | 
| 52 52 | 
             
            JSONDecodeError: Expecting value: line 1 column 1 (char 0)
         | 
| 53 53 |  | 
| 54 | 
            +
            ```
         | 
| 55 | 
            +
            ```python
         | 
| 56 | 
            +
            print(r.text)
         | 
| 57 | 
            +
            ↓
         | 
| 58 | 
            +
            <!DOCTYPE html>
         | 
| 59 | 
            +
            <html>
         | 
| 60 | 
            +
            <head>
         | 
| 61 | 
            +
              <title>The page you were looking for doesn't exist (404)</title>
         | 
| 62 | 
            +
              <meta name="viewport" content="width=device-width,initial-scale=1">
         | 
| 63 | 
            +
              <style>
         | 
| 64 | 
            +
              body {
         | 
| 65 | 
            +
                background-color: #EFEFEF;
         | 
| 66 | 
            +
                color: #2E2F30;
         | 
| 67 | 
            +
                text-align: center;
         | 
| 68 | 
            +
                font-family: arial, sans-serif;
         | 
| 69 | 
            +
                margin: 0;
         | 
| 70 | 
            +
              }
         | 
| 71 | 
            +
             | 
| 72 | 
            +
              div.dialog {
         | 
| 73 | 
            +
                width: 95%;
         | 
| 74 | 
            +
                max-width: 33em;
         | 
| 75 | 
            +
                margin: 4em auto 0;
         | 
| 76 | 
            +
              }
         | 
| 77 | 
            +
             | 
| 78 | 
            +
              div.dialog > div {
         | 
| 79 | 
            +
                border: 1px solid #CCC;
         | 
| 80 | 
            +
                border-right-color: #999;
         | 
| 81 | 
            +
                border-left-color: #999;
         | 
| 82 | 
            +
                border-bottom-color: #BBB;
         | 
| 83 | 
            +
                border-top: #B00100 solid 4px;
         | 
| 84 | 
            +
                border-top-left-radius: 9px;
         | 
| 85 | 
            +
                border-top-right-radius: 9px;
         | 
| 86 | 
            +
                background-color: white;
         | 
| 87 | 
            +
                padding: 7px 12% 0;
         | 
| 88 | 
            +
                box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
         | 
| 89 | 
            +
              }
         | 
| 90 | 
            +
             | 
| 91 | 
            +
              h1 {
         | 
| 92 | 
            +
                font-size: 100%;
         | 
| 93 | 
            +
                color: #730E15;
         | 
| 94 | 
            +
                line-height: 1.5em;
         | 
| 95 | 
            +
              }
         | 
| 96 | 
            +
             | 
| 97 | 
            +
              div.dialog > p {
         | 
| 98 | 
            +
                margin: 0 0 1em;
         | 
| 99 | 
            +
                padding: 1em;
         | 
| 100 | 
            +
                background-color: #F7F7F7;
         | 
| 101 | 
            +
                border: 1px solid #CCC;
         | 
| 102 | 
            +
                border-right-color: #999;
         | 
| 103 | 
            +
                border-left-color: #999;
         | 
| 104 | 
            +
                border-bottom-color: #999;
         | 
| 105 | 
            +
                border-bottom-left-radius: 4px;
         | 
| 106 | 
            +
                border-bottom-right-radius: 4px;
         | 
| 107 | 
            +
                border-top-color: #DADADA;
         | 
| 108 | 
            +
                color: #666;
         | 
| 109 | 
            +
                box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
         | 
| 110 | 
            +
              }
         | 
| 111 | 
            +
              </style>
         | 
| 112 | 
            +
            </head>
         | 
| 113 | 
            +
             | 
| 114 | 
            +
            <body>
         | 
| 115 | 
            +
              <!-- This file lives in public/404.html -->
         | 
| 116 | 
            +
              <div class="dialog">
         | 
| 117 | 
            +
                <div>
         | 
| 118 | 
            +
                  <h1>The page you were looking for doesn't exist.</h1>
         | 
| 119 | 
            +
                  <p>You may have mistyped the address or the page may have moved.</p>
         | 
| 120 | 
            +
                </div>
         | 
| 121 | 
            +
                <p>If you are the application owner check the logs for more information.</p>
         | 
| 122 | 
            +
              </div>
         | 
| 123 | 
            +
            </body>
         | 
| 124 | 
            +
            </html>
         | 
| 54 125 | 
             
            ```
         | 
2
変更点追加
    
        title	
    CHANGED
    
    | 
            File without changes
         | 
    
        body	
    CHANGED
    
    | 
            File without changes
         | 
1
コード追加
    
        title	
    CHANGED
    
    | @@ -1,1 +1,1 @@ | |
| 1 | 
            -
             | 
| 1 | 
            +
            python スクレイピング json.loads() JSONDecodeError
         | 
    
        body	
    CHANGED
    
    | 
            File without changes
         | 
