用变量更新url时出现urlopen错误

时间:2017-11-12 16:53:42

标签: python pandas variables urllib

我正在尝试使用其他文件中的关键字更新网址并阅读网址的内容但是却抛出了错误

            V1 2014.01.14 2014.04.14 2014.07.14 2014.10.14 2015.01.14 2015.04.14 2015.07.14 2015.10.14 2016.01.14
 1: 2014.01.14          6          6          5          5          5          4          3          3          3
 2: 2014.04.14          6          6          5          5          5          4          3          3          3
 3: 2014.07.14          5          5          5          5          5          4          3          3          3
 4: 2014.10.14          5          5          5          7          7          6          5          5          5
 5: 2015.01.14          5          5          5          7          8          7          6          6          6
 6: 2015.04.14          4          4          4          6          7          9          7          7          7
 7: 2015.07.14          3          3          3          5          6          7          9          8          6
 8: 2015.10.14          3          3          3          5          6          7          8          8          6
 9: 2016.01.14          3          3          3          5          6          7          6          6          8
10: 2016.04.14          2          2          2          3          4          4          3          3          5
11: 2016.07.14          4          4          3          4          5          4          3          3          4
12: 2016.10.14          4          4          3          3          4          3          2          2          3
13: 2017.01.06          4          4          4          4          5          4          4          4          3
14: 2017.03.06          4          4          4          4          5          4          4          4          3
    2016.04.14 2016.07.14 2016.10.14 2017.01.06 2017.03.06
 1:          2          4          4          4          4
 2:          2          4          4          4          4
 3:          2          3          3          4          4
 4:          3          4          3          4          4
 5:          4          5          4          5          5
 6:          4          4          3          4          4
 7:          3          3          2          4          4
 8:          3          3          2          4          4
 9:          5          4          3          3          3
10:          7          6          4          4          3
11:          6          8          6          5          4
12:          4          6          6          5          4
13:          4          5          5          7          6
14:          3          4          4          6          6

以下是错误

f = open('myfile.txt')
for line in iter(f):
    id = line.strip('\n')
    url_sell = 'https://example.com/getmarketsummary?market='+str(id)
    df = pd.read_json(url_sell, orient='columns')

1 个答案:

答案 0 :(得分:0)

使用try,excep to debug。

for line in iter(f):
    id = line.strip('\n')
    try:
        url_sell = 'https://example.com/getmarketsummary?market='+str(id)
    except:
        return id