(Correction) Python HTTP Error: Forbidden Question!

Asked 2 years ago, Updated 2 years ago, 58 views

I want to download it from the same address. There are too many to do manually, so I thought it would be simple, so I coded it.

It says HTTPError: Forbidden.

Without %06 just 1, 2, 3, 4... It worked well when it increased, but I needed a zero in front of it, so I kept getting errors when I put it in to match 6 digits.

In the middle, the %6d part is 000001... I'm going to download it by changing to 000100.

I googled it and they told me to put in the header, so I don't know if this is right, but I put it in. And yet, there's a Forbidden error.

Below is the source code we tried in two ways.

Help me.

import urllib.request

import requests

for i in range(100):

    url = f'https://abcd.com/ABC/BC-{i:06d}.txt?key=QAWBCR-AAA=3'

    req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})

    response = urllib.request.urlopen(req).read()

    text = response.decode('utf-8')

    urllib.request.urlretrieve(text, "C:\Tempp\{}.ts".format(i))
import urllib.request

import requests

for i in range(100):

    user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1;)'

    headers = {'User-Agent' : user_agent, }

    url = f'https://abcd.com/ABC/BC-{i:06d}.txt?key=QAWBCR-AAA=3'

    secu_url = urllib.request.urlretrieve(url, "C:\Tempp\{}.ts".format(i))

    request = urllib.request.Request(secu_url, None, headers)

python url error

2022-09-22 17:53

1 Answers

Please refer to the code below.

In [13]: for i in range(10): print(f'https://abcd.com/ABC/BC-{i:06d}.txt?key=QAWBCR-AAA=3')                                                                                                                                                                                     
https://abcd.com/ABC/BC-000000.txt?key=QAWBCR-AAA=3
https://abcd.com/ABC/BC-000001.txt?key=QAWBCR-AAA=3
https://abcd.com/ABC/BC-000002.txt?key=QAWBCR-AAA=3
https://abcd.com/ABC/BC-000003.txt?key=QAWBCR-AAA=3
https://abcd.com/ABC/BC-000004.txt?key=QAWBCR-AAA=3
https://abcd.com/ABC/BC-000005.txt?key=QAWBCR-AAA=3
https://abcd.com/ABC/BC-000006.txt?key=QAWBCR-AAA=3
https://abcd.com/ABC/BC-000007.txt?key=QAWBCR-AAA=3
https://abcd.com/ABC/BC-000008.txt?key=QAWBCR-AAA=3
https://abcd.com/ABC/BC-000009.txt?key=QAWBCR-AAA=3


2022-09-22 17:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.