Home  >  Q&A  >  body text

python - Added user-agent to solve 403, but then using urlretrieve prompts a regular matching error

I want to write a small program to automatically download the download link from the web page http://www.sse.com.cn/assortm... http://query.sse.com.cn/secur...
I used urllib to prompt 403, so I added user-agent to return 200, but then when using urlretrieve, it prompted a regular matching error. I couldn't find the answer online. How can you solve this problem?

code show as below:

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)

Error report:

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

迷茫迷茫2711 days ago1294

reply all(1)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-05-18 10:49:01

    Use request.build_opener to add head to solve the problem

    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)

    reply
    0
  • Cancelreply