Hướng dẫn python download all files from url - python tải xuống tất cả các tệp từ url

2 câu trả lời

Bạn có thể tải xuống tệp như thế này

import urllib2
response = urllib2.urlopen['//www.example.com/file_to_download']
html = response.read[]

Để có tất cả các liên kết trong một trang

from bs4 import BeautifulSoup

import requests
r  = requests.get["//site-to.crawl"]
data = r.text
soup = BeautifulSoup[data]

for link in soup.find_all['a']:
    print[link.get['href']]

Đã trả lời ngày 7 tháng 8 năm 2017 lúc 6:59Aug 7, 2017 at 6:59

Narennarennaren

14.1k5 Huy hiệu vàng37 Huy hiệu bạc44 Huy hiệu đồng5 gold badges37 silver badges44 bronze badges

0

Đã trả lời ngày 20 tháng 6 năm 2020 lúc 23:32Jun 20, 2020 at 23:32

x89x89x89

2.0664 Huy hiệu vàng25 Huy hiệu bạc70 Huy hiệu Đồng4 gold badges25 silver badges70 bronze badges

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:

  • Vì tất cả dữ liệu tệp có thể được lưu trữ bởi một chuỗi duy nhất, chúng tôi sử dụng phương thức r.iter_content để tải dữ liệu theo khối, chỉ định kích thước chunk.r.iter_content method to load data in chunks, specifying the chunk size.
  •  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 requests 

    from bs4 import BeautifulSoup 

    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    0
    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    1

    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    2
    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    3
    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    4
    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    5

    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    2
    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    7
    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    4
    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    9
    pip install requests
    0
    pip install requests
    1

    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    2
    pip install requests
    3
    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    4
    pip install requests
    5
    pip install requests
    6
    pip install requests
    1

    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    2
    pip install requests
    9
    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    4
     r = requests.get[URL, stream = True]
    1
     r = requests.get[URL, stream = True]
    2
     r = requests.get[URL, stream = True]
    3__

    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    2import7 import8

    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    0 requests 0

    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    2
     r = requests.get[URL, stream = True]
    6
     r = requests.get[URL, stream = True]
    7
     r = requests.get[URL, stream = True]
    8 requests 5

    requests 6requests 7

    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    4 requests 9from0from1from2from3from4

    requests 6from6from7from8from9bs4 0

    requests 6

    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    3
    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    4 bs4 4
    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    4 bs4 6
    pip install requests
    1

    requests 6bs4 9import0import1import2import3

    import4

     r = requests.get[URL, stream = True]
    6 import6
     r = requests.get[URL, stream = True]
    8 import8__

    BeautifulSoup 4import0 BeautifulSoup 6

    BeautifulSoup 7BeautifulSoup 8

    requests 6from6from7

    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    02from9
    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    04

    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    2from6
    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    07
    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    08
    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    09

    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    2import7

    import0

    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    13
    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    4
    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    4
    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    16
    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    17

    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    2
    pip install requests
    9
    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    4
    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    21

    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    2
    from bs4 import BeautifulSoup
    
    import requests
    r  = requests.get["//site-to.crawl"]
    data = r.text
    soup = BeautifulSoup[data]
    
    for link in soup.find_all['a']:
        print[link.get['href']]
    
    23

    Ư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.


    Làm thế nào tải xuống tất cả các tập tin từ trang web Python?

    Nhập mô -đun. Nhập yêu cầu ..
    Nhận liên kết hoặc URL. url = '//www.facebook.com/favicon.ico' r = requests.get [url, allow_redirects = true].
    Lưu nội dung với tên. Mở ['Facebook.ICO', 'WB']. Viết [R.Content] lưu tệp dưới dạng Facebook. ....
    Nhận tên tệp từ một url. Để có được tên tệp, chúng ta có thể phân tích URL ..

    Làm cách nào để tải xuống nhiều tệp từ URL?

    Cách tải xuống nhiều tập tin web..
    Chạy tải xuống nhiều phần mềm tệp web ..
    Nhấp vào nút "Thêm URL" để thêm URL trang web.Nút "Tải từ tệp" tải tất cả các URL trang web trong một tệp.....
    Để các thư mục đích, nhấp vào "Thêm [các] thư mục" để thêm thư mục.....
    Để bắt đầu tải xuống các tệp web, nhấp vào nút "Tải xuống tệp web" ..

    Làm thế nào để bạn tải xuống dữ liệu từ một url trong python?

    Để tải xuống một tệp từ URL bằng Python, hãy làm theo ba bước sau:..
    Cài đặt mô -đun yêu cầu và nhập nó vào dự án của bạn ..
    Sử dụng yêu cầu.Nhận [] để tải xuống dữ liệu đằng sau url đó ..
    Viết tệp vào tệp trong hệ thống của bạn bằng cách gọi Open [] ..

    Làm cách nào để tải xuống nhiều URL từ Python?

    # Tải xuống tất cả các tập tin từ một trang web tuần tự.từ HĐH nhập khẩu makedirs ..
    từ hệ điều hành.Đường dẫn nhập cơ sở.từ hệ điều hành.Tham gia nhập khẩu đường dẫn ..
    từ Urllib.Yêu cầu nhập urlopen.từ Urllib.Nhập Urljoin nhập phân tích ..
    Từ BS4 Nhập cảnh đẹp ..
    # Tải tệp từ URL, trả về nội dung của tệp đã tải xuống.DEF download_url [urlpath]:.

    Bài Viết Liên Quan

    Chủ Đề