Hướng dẫn beautifulsoup python geeksforgeeks - Beautifulsoup python geeksforgeeks
Chủ yếu có hai cách để trích xuất dữ liệu từ một trang web: Show
Bài viết này thảo luận về các bước liên quan đến việc cạo web bằng cách sử dụng việc triển khai khung cạo web của Python gọi là Soup xinh đẹp. Các bước liên quan đến việc quét web:Steps involved in web scraping:
Bước 1: Cài đặt các thư viện bên thứ ba cần thiết
pip install requests pip install html5lib pip install bs4
Bước 2: Truy cập nội dung HTML từ WebPage & NBSP; PythonHãy để chúng tôi cố gắng hiểu đoạn mã này.
Lưu ý: Đôi khi, bạn có thể gặp lỗi không được chấp nhận, vì vậy hãy thử thêm một tác nhân người dùng trình duyệt như bên dưới. Tìm tác nhân người dùng của bạn dựa trên thiết bị và trình duyệt từ đây https://deviceatlas.com/blog/list-of-user-agent-strings Python3
soup = BeautifulSoup(r.content, 'html5lib')0 soup = BeautifulSoup(r.content, 'html5lib')1 soup = BeautifulSoup(r.content, 'html5lib')2 soup = BeautifulSoup(r.content, 'html5lib')3 ‘ table = soup.find('div', attrs = {'id':'all_quotes'})1 table = soup.find('div', attrs = {'id':'all_quotes'})2 Bước 3: Phân tích nội dung HTML & NBSP; PythonHãy để chúng tôi cố gắng hiểu đoạn mã này. soup = BeautifulSoup(r.content, 'html5lib') Trước hết nhập thư viện yêu cầu.
Bây giờ, như in r.content cho & nbsp; lấy nội dung HTML thô của trang web. Nó thuộc loại ‘chuỗi.soup.prettify() is printed, it gives the visual representation of the parse tree created from the raw HTML content. Step 4: Searching and navigating through the parse tree Now, we would like to extract some useful data from the HTML content. The soup object contains all the data in the nested structure which could be programmatically extracted. In our example, we are scraping a webpage consisting of some quotes. So, we would like to create a program to save those quotes (and all relevant information about them). PythonHãy để chúng tôi cố gắng hiểu đoạn mã này. Trước hết nhập thư viện yêu cầu. Sau đó, chỉ định URL của trang web bạn & nbsp; muốn cạo. Gửi yêu cầu HTTP đến URL được chỉ định và lưu phản hồi từ máy chủ trong một đối tượng phản hồi gọi là r. Bây giờ, như in r.content cho & nbsp; lấy nội dung HTML thô của trang web. Nó thuộc loại ‘chuỗi. for row in table.find_all_next('div', attrs = {'class': 'col-6 col-lg-3 text-center margin-30px-bottom sm-margin-30px-top'}): quote = {} quote['theme'] = row.h5.text quote['url'] = row.a['href'] quote['img'] = row.img['src'] quote['lines'] = row.img['alt'].split(" #")[0] quote['author'] = row.img['alt'].split(" #")[1] quotes.append(quote)9 = quote['theme'] = row.h5.text1 Lưu ý: Đôi khi, bạn có thể gặp lỗi không được chấp nhận, vì vậy hãy thử thêm một tác nhân người dùng trình duyệt như bên dưới. Tìm tác nhân người dùng của bạn dựa trên thiết bị và trình duyệt từ đây https://deviceatlas.com/blog/list-of-user-agent-strings
soup = BeautifulSoup(r.content, 'html5lib')0 soup = BeautifulSoup(r.content, 'html5lib')1 soup = BeautifulSoup(r.content, 'html5lib')2 soup = BeautifulSoup(r.content, 'html5lib')3 ‘ Bước 3: Phân tích nội dung HTML & NBSP; Một điều thực sự hay về thư viện đẹp là nó được xây dựng trên đỉnh của các thư viện phân tích cú pháp HTML như HTML5LIB, LXML, HTML.PARSER, v.v. Trong ví dụ trên, Chúng tôi tạo một đối tượng đẹp bằng cách truyền hai đối số: R.Content: Đó là nội dung HTML thô. html5lib & nbsp ;: Chỉ định trình phân tích cú pháp HTML mà chúng tôi muốn sử dụng. Bây giờ súp.prettify () được in, & nbsp; nó cung cấp biểu diễn trực quan của cây phân tích được tạo từ nội dung HTML thô. Bước 4: Tìm kiếm và điều hướng qua cây Parse Bây giờ, chúng tôi muốn trích xuất một số dữ liệu hữu ích từ nội dung HTML. Đối tượng súp chứa tất cả các dữ liệu trong cấu trúc lồng nhau có thể được trích xuất theo chương trình. Trong ví dụ của chúng tôi, chúng tôi đang thực hiện một trang web bao gồm một số trích dẫn. Vì vậy, chúng tôi muốn tạo một chương trình để lưu các trích dẫn đó (và tất cả các thông tin liên quan về chúng). & NBSP; filename = 'inspirational_quotes.csv' with open(filename, 'w', newline='') as f: w = csv.DictWriter(f,['theme','url','img','lines','author']) w.writeheader() for quote in quotes: w.writerow(quote)7 soup = BeautifulSoup(r.content, 'html5lib')18 table = soup.find('div', attrs = {'id':'all_quotes'})3 table = soup.find('div', attrs = {'id':'all_quotes'})4 soup = BeautifulSoup(r.content, 'html5lib')22 soup = BeautifulSoup(r.content, 'html5lib')23 soup = BeautifulSoup(r.content, 'html5lib')24 soup = BeautifulSoup(r.content, 'html5lib')25 soup = BeautifulSoup(r.content, 'html5lib')26 = soup = BeautifulSoup(r.content, 'html5lib')28 filename = 'inspirational_quotes.csv' with open(filename, 'w', newline='') as f: w = csv.DictWriter(f,['theme','url','img','lines','author']) w.writeheader() for quote in quotes: w.writerow(quote)7 soup = BeautifulSoup(r.content, 'html5lib')30 = soup = BeautifulSoup(r.content, 'html5lib')32 headers 3quote['url'] = row.a['href']8 headers 9quote['url'] = row.a['href']8 = 7quote['url'] = row.a['href']8__95958__1079 filename = 'inspirational_quotes.csv' with open(filename, 'w', newline='') as f: w = csv.DictWriter(f,['theme','url','img','lines','author']) w.writeheader() for quote in quotes: w.writerow(quote)7 soup = BeautifulSoup(r.content, 'html5lib')44 filename = 'inspirational_quotes.csv' with open(filename, 'w', newline='') as f: w = csv.DictWriter(f,['theme','url','img','lines','author']) w.writeheader() for quote in quotes: w.writerow(quote)7 quote['url'] = row.a['href']3 filename = 'inspirational_quotes.csv' with open(filename, 'w', newline='') as f: w = csv.DictWriter(f,['theme','url','img','lines','author']) w.writeheader() for quote in quotes: w.writerow(quote)8 quote['url'] = row.a['href']5 soup = BeautifulSoup(r.content, 'html5lib')49 soup = BeautifulSoup(r.content, 'html5lib')50 soup = BeautifulSoup(r.content, 'html5lib')51 Trước khi tiếp tục, chúng tôi khuyên bạn nên xem qua nội dung HTML của trang web mà chúng tôi đã in bằng phương thức súp.prettify () và cố gắng tìm một mẫu hoặc cách điều hướng đến các trích dẫn.
table = soup.find('div', attrs = {'id':'all_quotes'})
for row in table.find_all_next('div', attrs = {'class': 'col-6 col-lg-3 text-center margin-30px-bottom sm-margin-30px-top'}): quote = {} quote['theme'] = row.h5.text quote['url'] = row.a['href'] quote['img'] = row.img['src'] quote['lines'] = row.img['alt'].split(" #")[0] quote['author'] = row.img['alt'].split(" #")[1] quotes.append(quote)
quote['theme'] = row.h5.text
quote['url'] = row.a['href']
filename = 'inspirational_quotes.csv' with open(filename, 'w', newline='') as f: w = csv.DictWriter(f,['theme','url','img','lines','author']) w.writeheader() for quote in quotes: w.writerow(quote)
Vì vậy, đây là một ví dụ đơn giản về cách tạo ra một cái cào web trong Python. & nbsp; từ đây, bạn có thể cố gắng loại bỏ bất kỳ trang web nào khác mà bạn chọn. Trong trường hợp của bất kỳ truy vấn nào, hãy đăng chúng dưới đây trong phần bình luận. LƯU Ý: Xóa web được coi là bất hợp pháp trong nhiều trường hợp. Nó cũng có thể khiến IP của bạn bị chặn vĩnh viễn bởi một trang web. 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. Web Scraping is considered as illegal in many cases. It may also cause your IP to be blocked permanently by a website. This blog is contributed by Nikhil Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. |