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:
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']]
1from 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']]
2from 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']]
3from 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']]
5from 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']]
2from 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']]
7from 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']]
9pip install requests0
pip install requests1
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']]
2pip install requests3
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 requests5
pip install requests6
pip install requests1
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']]
2pip install requests9
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']]
2import
7 import
8from 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
0from 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']]
2r = requests.get[URL, stream = True]6
r = requests.get[URL, stream = True]7
r = requests.get[URL, stream = True]8
requests
5requests
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
9from
0from
1from
2from
3from
4requests
6from
6from
7from
8from
9bs4
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']]
3from 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
4from 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
6pip install requests1
requests
6bs4
9import
0import
1import
2import
3
import
4
r = requests.get[URL, stream = True]6
import
6r = requests.get[URL, stream = True]8
import
8__BeautifulSoup
4import
0 BeautifulSoup
6
BeautifulSoup
7BeautifulSoup
8
requests
6from
6from
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']]
02from
9from 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']]
04from 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']]
2from
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']]
07from 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']]
08from 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']]
09from 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']]
2import
7import
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']]
13from 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']]
4from 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']]
16from 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']]
17from 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']]
2pip install requests9
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']]
21from 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']]
2from 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.