-2

I am struggling how to download all albums in a website. Manually, I would click each album, which takes me to the album page and then click download. This is laborious.

Code:

import urllib.request 
tar_url = "https:songs-download"

data = urllib.request.Request(tar_url)
for line in data:
  print(line)

Present output:

TypeError: 'Request' object is not iterable
Mainland
  • 4,110
  • 3
  • 25
  • 56

1 Answers1

1

To iterate through data you'll need to use data.read() and then iterate through that like so:

for line in data.read():
print(line)

try taking a look at the docs for requests https://docs.python.org/3/library/urllib.request.html

JJaco
  • 61
  • 1
  • 11
  • I got following error `AttributeError: 'Request' object has no attribute 'content` – Mainland Jul 07 '22 at 02:48
  • After referring the link you suggested, I got the following working `import requests data = requests.get(tar_url) print(data.text) for line in data.content: print(line)` But don't know how to download? – Mainland Jul 07 '22 at 02:55
  • `AttributeError: 'Request' object has no attribute 'read'` – Mainland Jul 07 '22 at 02:57
  • @Mainland downloading is another problem you will have to solve try collecting all links into a list and downloading them. This page should help you https://stackoverflow.com/questions/31954687/how-to-use-a-download-link-to-download-a-file-in-python – JJaco Jul 07 '22 at 02:59