python - 為解決403 加了user-agent,但之後使用urlretrieve就提示正則匹配錯誤
迷茫
迷茫 2017-05-18 10:47:01
0
1
1337

想寫一個小程式自動下載網頁http://www.sse.com.cn/assortm...裡面的下載連結 http://query.sse.com.cn/secur...
用urllib提示403,於是加了user-agent回傳200,但之後使用urlretrieve就提示正規符合錯誤,網路上沒找到答案,請問大家要怎麼解決這個問題?

程式碼如下:

from urllib import request

from datetime import datetime

-- coding:utf-8 -

-

#url = 'http://query.sse.com.cn/secur...'

user_agent = 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Mobile Safari/537.36'

##myheaders = {'User - Agent': user_agent}

#req = request.Request(url, headers=myheaders)

local = "/Users/Mty/Downloads/s_data/" str(datetime.now().date()) " .xls"

request.urlretrieve(req, local)

報錯:


Traceback (most recent call last):

File "/Users/Mty/PycharmProjects/get_data/date.py", line 20, in <module>

request.urlretrieve(req, local)

File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 186, in urlretrieve

url_type, path = splittype(url)

File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/parse.py", line 861, in splittype

match = _typeprog.match(url)

TypeError: expected string or bytes-like object###
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全部回覆(1)
淡淡烟草味

使用request.build_opener 新增head可解決

myheaders = [('User - Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.2) AppleWebKit/525.17'
                              ' (KHTML, like Gecko) Version/3.1 Safari/525.17'),]
opener = request.build_opener()
opener.addheaders = myheaders
request.install_opener(opener)

request.urlretrieve(url, local)
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板