multithreadinginpython
multithreadinginpython copied to clipboard
HTTPError
HTTPError due to missing header
#Use this code, add headers import json import urllib.request import time header= {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) ' 'AppleWebKit/537.11 (KHTML, like Gecko) ' 'Chrome/23.0.1271.64 Safari/537.11', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8', 'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', 'Accept-Encoding': 'none', 'Accept-Language': 'en-US,en;q=0.8', 'Connection': 'keep-alive'}
def count_letters(url, frequency): #the URL where you are requesting at req = urllib.request.Request(url=url, headers=header) response = urllib.request.urlopen(req) txt = str(response.read()) for l in txt: letter = l.lower() if letter in frequency: frequency[letter] += 1
def main(): frequency = {} for c in "abcdefghijklmnopqrstuvwxyz": frequency[c] = 0 start = time.time() for i in range(1000, 1020): count_letters(f"https://www.rfc-editor.org/rfc/rfc{i}.txt", frequency) end = time.time() print(json.dumps(frequency, indent=4)) print("Done, time taken", end - start)
main()