Bạn không cần sử dụng urllib.URLopener[]
, chỉ cần sử dụng urllib.urlretrieve[]
như sau:
import urllib
urllib.urlretrieve["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
Bạn cũng có thể sử dụng wget
:
import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
Và về việc tự động hóa nó để tải xuống hàng ngày, bạn có thể sử dụng trình lập lịch Windows hoặc sử dụng loop
như sau:
import urllib
import time
while True:
urllib.urlretrieve['file', 'file']
time.sleep[86400] # 86400 seconds = 24 hours.
Yêu cầu là một thư viện HTTP đa năng trong Python với các ứng dụng khác nhau. Một trong những ứng dụng của nó là tải xuống một tệp từ Web bằng tệp URL.installation: Trước hết, bạn sẽ cần tải xuống thư viện yêu cầu. Bạn có thể trực tiếp cài đặt nó bằng PIP bằng cách nhập lệnh sau:
Installation: First of all, you would need to download the requests library. You can directly install it using pip by typing following command:
pip install requests
Hoặc tải xuống trực tiếp từ đây và cài đặt thủ công.
Tải tập tin
Món mã nhỏ được viết ở trên sẽ tải xuống hình ảnh sau từ web. Bây giờ hãy kiểm tra thư mục cục bộ của bạn [thư mục nơi tập lệnh này cư trú] và bạn sẽ tìm thấy hình ảnh này:
Tất cả những gì chúng ta cần là URL của nguồn hình ảnh. [Bạn có thể lấy URL của nguồn hình ảnh bằng cách nhấp chuột phải vào hình ảnh và chọn tùy chọn xem hình ảnh.]
Tải xuống các tập tin lớn
Nội dung phản hồi HTTP [R.Content] không là gì ngoài một chuỗi đang lưu trữ dữ liệu tệp. Vì vậy, nó đã giành chiến thắng để lưu tất cả dữ liệu trong một chuỗi trong trường hợp các tệp lớn. Để khắc phục vấn đề này, chúng tôi thực hiện một số thay đổi cho chương trình của mình:r.content] is nothing but a string which is storing the file data. So, it won’t be possible to save all the data in a single string in case of large files. To overcome this problem, we do some changes to our program:
r = requests.get[URL, stream = True]
Đặt tham số luồng thành true sẽ chỉ khiến việc tải xuống các tiêu đề phản hồi và kết nối vẫn mở. Điều này tránh đọc tất cả nội dung cùng một lúc vào bộ nhớ cho các phản hồi lớn. Một khối cố định sẽ được tải mỗi lần trong khi r.iter_content được lặp lại.stream parameter to True will cause the download of response headers only and the connection remains open. This avoids reading the content all at once into memory for large responses. A fixed chunk will be loaded each time while r.iter_content is iterated.
Đây là một ví dụ:
Tải xuống video
Trong ví dụ này, chúng tôi quan tâm đến việc tải xuống tất cả các bài giảng video có sẵn trên trang web này. Tất cả các tài liệu lưu trữ của bài giảng này có sẵn ở đây. Vì vậy, trước tiên chúng tôi xóa trang web để trích xuất tất cả các liên kết video và sau đó tải xuống từng video.
import
import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
0import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
1 import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
2import
import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
4import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
5 import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
6import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
7import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
8import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
9 import urllib
import time
while True:
urllib.urlretrieve['file', 'file']
time.sleep[86400] # 86400 seconds = 24 hours.
0import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
7import urllib
import time
while True:
urllib.urlretrieve['file', 'file']
time.sleep[86400] # 86400 seconds = 24 hours.
22import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
9 import urllib
import time
while True:
urllib.urlretrieve['file', 'file']
time.sleep[86400] # 86400 seconds = 24 hours.
4import urllib
import time
while True:
urllib.urlretrieve['file', 'file']
time.sleep[86400] # 86400 seconds = 24 hours.
5import urllib
import time
while True:
urllib.urlretrieve['file', 'file']
time.sleep[86400] # 86400 seconds = 24 hours.
6import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
7import urllib
import time
while True:
urllib.urlretrieve['file', 'file']
time.sleep[86400] # 86400 seconds = 24 hours.
8import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
9 pip install requests0
pip install requests1
import urllib
import time
while True:
urllib.urlretrieve['file', 'file']
time.sleep[86400] # 86400 seconds = 24 hours.
6import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
7pip install requests4
import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
9 pip install requests6
pip install requests7
pip install requests8
pip install requests9__4041
import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
7urllib.URLopener[]
2 urllib.URLopener[]
3import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
5 urllib.URLopener[]
5import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
7r = requests.get[URL, stream = True]1
r = requests.get[URL, stream = True]2
r = requests.get[URL, stream = True]3
urllib.urlretrieve[]
0urllib.urlretrieve[]
1urllib.urlretrieve[]
2
import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
9 urllib.urlretrieve[]
4urllib.urlretrieve[]
5urllib.urlretrieve[]
6urllib.urlretrieve[]
7urllib.urlretrieve[]
8‘
urllib.urlretrieve[]
1
import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
8import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
9 wget
9import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
9 loop
1import urllib
import time
while True:
urllib.urlretrieve['file', 'file']
time.sleep[86400] # 86400 seconds = 24 hours.
6urllib.urlretrieve[]
1loop
4loop
5loop
6loop
7loop
8
loop
9
r = requests.get[URL, stream = True]1
import
1r = requests.get[URL, stream = True]3
import
3__import
9
r = requests.get[URL, stream = True]5
import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
01import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
02import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
03urllib.urlretrieve[]
1wget
1wget
2
import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
07wget
4import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
09import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
7wget
1 import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
122____113import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
14import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
7urllib.URLopener[]
2r = requests.get[URL, stream = True]5
import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
18import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
9import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
9 import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
21import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
22import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
7pip install requests4
import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
9 import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
26import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
7import wget
wget.download["//www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8"]
28Ưu điểm của việc sử dụng thư viện yêu cầu để tải xuống các tệp web là:
- Người ta có thể dễ dàng tải xuống các thư mục web bằng cách lặp lại đệ quy thông qua trang web!
- Đây là một phương pháp độc lập với trình duyệt và nhanh hơn nhiều!
- Người ta có thể chỉ cần xóa một trang web để lấy tất cả các URL tệp trên trang web và do đó, tải xuống tất cả các tệp trong một lệnh-
Thực hiện quét web trong Python với BeautifulSoup
Blog này được đóng góp bởi Nikhil Kumar. Nếu bạn thích GeekSforGeeks và muốn đóng góp, bạn cũng có thể viết một bài viết bằng Write.GeekSforGeek.org hoặc gửi bài viết của bạn. Xem bài viết của bạn xuất hiện trên trang chính của GeekSforGeek và giúp các chuyên viên máy tính khác.
Vui lòng viết nhận xét nếu bạn tìm thấy bất cứ điều gì không chính xác, hoặc bạn muốn chia sẻ thêm thông tin về chủ đề được thảo luận ở trên.