Bạn có thể sử dụng Python để trích xuất dữ liệu không?

Python là một ngôn ngữ đẹp để viết mã. Nó có một hệ sinh thái gói tuyệt vời, ít tiếng ồn hơn nhiều so với các ngôn ngữ khác và nó cực kỳ dễ sử dụng

Python được sử dụng cho nhiều thứ, từ phân tích dữ liệu đến lập trình máy chủ. Và một trường hợp sử dụng thú vị của Python là Web Scraping

Trong bài viết này, chúng tôi sẽ đề cập đến cách sử dụng Python để quét web. Chúng tôi cũng sẽ làm việc thông qua hướng dẫn lớp học thực hành hoàn chỉnh khi chúng tôi tiến hành

Ghi chú. Chúng tôi sẽ cạo một trang web mà tôi lưu trữ, vì vậy chúng tôi có thể học cách cạo trên đó một cách an toàn. Nhiều công ty không cho phép cạo trên trang web của họ, vì vậy đây là một cách tốt để tìm hiểu. Chỉ cần đảm bảo kiểm tra trước khi cạo

Giới thiệu về lớp học Web Scraping

Xem trước lớp học codedamn

Nếu bạn muốn học viết mã, bạn có thể sử dụng lớp học viết mã miễn phí này bao gồm nhiều phòng thí nghiệm để giúp bạn tìm hiểu cách quét web. Đây sẽ là một bài học thực hành thực tế trên codedamn, tương tự như cách bạn học trên freeCodeCamp

Trong lớp học này, bạn sẽ sử dụng trang này để kiểm tra việc quét web. https. //codedamn-lớp học. github. io/webscraper-python-codedamn-classroom-website/

Lớp học này bao gồm 7 phòng thí nghiệm và bạn sẽ giải quyết một phòng thí nghiệm trong mỗi phần của bài đăng trên blog này. Chúng tôi sẽ sử dụng Python 3. 8 + BeautifulSoup 4 để quét web

Phần 1. Đang tải các trang web có 'yêu cầu'

Đây là liên kết đến phòng thí nghiệm này

Mô-đun import requests # Make a request to //codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/ # Store the result in 'res' variable res = requests.get( '//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/') txt = res.text status = res.status_code print(txt, status) # print the result5 cho phép bạn gửi các yêu cầu HTTP bằng Python

Yêu cầu HTTP trả về một Đối tượng phản hồi với tất cả dữ liệu phản hồi (nội dung, mã hóa, trạng thái, v.v.). Một ví dụ về việc lấy HTML của một trang

import requests res = requests.get('//codedamn.com') print(res.text) print(res.status_code)

vượt qua yêu cầu

  • Nhận nội dung của URL sau bằng cách sử dụng mô-đun import requests # Make a request to //codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/ # Store the result in 'res' variable res = requests.get( '//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/') txt = res.text status = res.status_code print(txt, status) # print the result5. https. //codedamn-lớp học. github. io/webscraper-python-codedamn-classroom-website/
  • Lưu trữ phản hồi văn bản (như được hiển thị ở trên) trong một biến có tên là import requests # Make a request to //codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/ # Store the result in 'res' variable res = requests.get( '//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/') txt = res.text status = res.status_code print(txt, status) # print the result7
  • Lưu trữ mã trạng thái (như được hiển thị ở trên) trong một biến có tên là import requests # Make a request to //codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/ # Store the result in 'res' variable res = requests.get( '//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/') txt = res.text status = res.status_code print(txt, status) # print the result8
  • In import requests # Make a request to //codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/ # Store the result in 'res' variable res = requests.get( '//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/') txt = res.text status = res.status_code print(txt, status) # print the result7 và import requests # Make a request to //codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/ # Store the result in 'res' variable res = requests.get( '//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/') txt = res.text status = res.status_code print(txt, status) # print the result8 sử dụng chức năng from bs4 import BeautifulSoup page = requests.get("//codedamn.com") soup = BeautifulSoup(page.content, 'html.parser') title = soup.title.text # gets you the text of the <title>(...)</title>1

Khi bạn hiểu điều gì đang xảy ra trong đoạn mã trên, việc vượt qua phòng thí nghiệm này khá đơn giản. Đây là giải pháp cho phòng thí nghiệm này

import requests # Make a request to //codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/ # Store the result in 'res' variable res = requests.get( '//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/') txt = res.text status = res.status_code print(txt, status) # print the result

Bây giờ hãy chuyển sang phần 2, nơi bạn sẽ xây dựng thêm trên mã hiện tại của mình

Phần 2. Trích xuất tiêu đề với BeautifulSoup

Đây là liên kết đến phòng thí nghiệm này

Trong toàn bộ lớp học này, bạn sẽ sử dụng một thư viện có tên là from bs4 import BeautifulSoup page = requests.get("//codedamn.com") soup = BeautifulSoup(page.content, 'html.parser') title = soup.title.text # gets you the text of the <title>(...)</title>2 bằng Python để quét web. Một số tính năng làm cho BeautifulSoup trở thành một giải pháp mạnh mẽ là

  1. Nó cung cấp rất nhiều phương thức đơn giản và thành ngữ Pythonic để điều hướng, tìm kiếm và sửa đổi cây DOM. Không mất nhiều mã để viết một ứng dụng
  2. Beautiful Soup nằm trên các trình phân tích cú pháp Python phổ biến như lxml và html5lib, cho phép bạn thử các chiến lược phân tích cú pháp khác nhau hoặc thay đổi tốc độ để linh hoạt

Về cơ bản, BeautifulSoup có thể phân tích bất kỳ thứ gì trên web mà bạn cung cấp

Đây là một ví dụ đơn giản về BeautifulSoup

from bs4 import BeautifulSoup page = requests.get("//codedamn.com") soup = BeautifulSoup(page.content, 'html.parser') title = soup.title.text # gets you the text of the <title>(...)</title>

vượt qua yêu cầu

  • Sử dụng gói import requests # Make a request to //codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/ # Store the result in 'res' variable res = requests.get( '//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/') txt = res.text status = res.status_code print(txt, status) # print the result5 để lấy tiêu đề của URL. https. //codedamn-lớp học. github. io/webscraper-python-codedamn-classroom-website/
  • Sử dụng BeautifulSoup để lưu trữ tiêu đề của trang này vào một biến có tên là from bs4 import BeautifulSoup page = requests.get("//codedamn.com") soup = BeautifulSoup(page.content, 'html.parser') title = soup.title.text # gets you the text of the <title>(...)</title>4

Nhìn vào ví dụ trên, bạn có thể thấy khi chúng tôi cung cấp from bs4 import BeautifulSoup page = requests.get("//codedamn.com") soup = BeautifulSoup(page.content, 'html.parser') title = soup.title.text # gets you the text of the <title>(...)</title>5 bên trong BeautifulSoup, bạn có thể bắt đầu làm việc với cây DOM được phân tích cú pháp theo cách rất Pythonic. Giải pháp cho phòng thí nghiệm sẽ là

import requests from bs4 import BeautifulSoup # Make a request to //codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/ page = requests.get( "//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title.text # print the result print(page_title)

Đây cũng là một phòng thí nghiệm đơn giản, nơi chúng tôi phải thay đổi URL và in tiêu đề trang. Mã này sẽ vượt qua phòng thí nghiệm

Phần 3. Soup-ed cơ thể và đầu

Đây là liên kết đến phòng thí nghiệm này

Trong phòng thí nghiệm cuối cùng, bạn đã thấy cách bạn có thể trích xuất from bs4 import BeautifulSoup page = requests.get("//codedamn.com") soup = BeautifulSoup(page.content, 'html.parser') title = soup.title.text # gets you the text of the <title>(...)</title>6 từ trang. Việc trích xuất một số phần cũng dễ dàng không kém

Bạn cũng thấy rằng bạn phải gọi from bs4 import BeautifulSoup page = requests.get("//codedamn.com") soup = BeautifulSoup(page.content, 'html.parser') title = soup.title.text # gets you the text of the <title>(...)</title>7 trên những thứ này để lấy chuỗi, nhưng bạn cũng có thể in chúng mà không cần gọi from bs4 import BeautifulSoup page = requests.get("//codedamn.com") soup = BeautifulSoup(page.content, 'html.parser') title = soup.title.text # gets you the text of the <title>(...)</title>7 và nó sẽ cung cấp cho bạn đánh dấu đầy đủ. Hãy thử chạy ví dụ dưới đây

import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn.com") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title.text # Extract body of page page_body = soup.body # Extract head of page page_head = soup.head # print the result print(page_body, page_head)

Hãy xem cách bạn có thể trích xuất các phần from bs4 import BeautifulSoup page = requests.get("//codedamn.com") soup = BeautifulSoup(page.content, 'html.parser') title = soup.title.text # gets you the text of the <title>(...)</title>9 và import requests from bs4 import BeautifulSoup # Make a request to //codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/ page = requests.get( "//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title.text # print the result print(page_title)0 từ các trang của mình

vượt qua yêu cầu

  • Lặp lại thử nghiệm với URL. import requests from bs4 import BeautifulSoup # Make a request to //codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/ page = requests.get( "//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title.text # print the result print(page_title)1
  • Lưu trữ tiêu đề trang (không gọi. văn bản) của URL trong from bs4 import BeautifulSoup page = requests.get("//codedamn.com") soup = BeautifulSoup(page.content, 'html.parser') title = soup.title.text # gets you the text of the <title>(...)</title>4
  • Lưu trữ nội dung cơ thể (không gọi. văn bản) của URL trong import requests from bs4 import BeautifulSoup # Make a request to //codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/ page = requests.get( "//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title.text # print the result print(page_title)3
  • Lưu trữ nội dung head (không gọi. văn bản) của URL trong import requests from bs4 import BeautifulSoup # Make a request to //codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/ page = requests.get( "//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title.text # print the result print(page_title)4

Khi bạn cố gắng in import requests from bs4 import BeautifulSoup # Make a request to //codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/ page = requests.get( "//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title.text # print the result print(page_title)3 hoặc import requests from bs4 import BeautifulSoup # Make a request to //codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/ page = requests.get( "//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title.text # print the result print(page_title)4, bạn sẽ thấy rằng chúng được in dưới dạng import requests from bs4 import BeautifulSoup # Make a request to //codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/ page = requests.get( "//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title.text # print the result print(page_title)7. Nhưng trong thực tế, khi bạn import requests from bs4 import BeautifulSoup # Make a request to //codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/ page = requests.get( "//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title.text # print the result print(page_title)8 bạn sẽ thấy nó không phải là một chuỗi mà nó hoạt động tốt

Giải pháp của ví dụ này sẽ đơn giản, dựa trên đoạn mã trên

import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title # Extract body of page page_body = soup.body # Extract head of page page_head = soup.head # print the result print(page_title, page_head)

Phần 4. chọn với BeautifulSoup

Đây là liên kết đến phòng thí nghiệm này

Bây giờ bạn đã khám phá một số phần của BeautifulSoup, hãy xem cách bạn có thể chọn các thành phần DOM bằng các phương thức BeautifulSoup

Sau khi bạn có biến import requests from bs4 import BeautifulSoup # Make a request to //codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/ page = requests.get( "//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title.text # print the result print(page_title)9 (như các phòng thí nghiệm trước), bạn có thể làm việc với biến import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn.com") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title.text # Extract body of page page_body = soup.body # Extract head of page page_head = soup.head # print the result print(page_body, page_head)0 trên đó, đây là bộ chọn CSS bên trong BeautifulSoup. Tức là bạn có thể với tới cây DOM giống như cách bạn sẽ chọn phần tử bằng CSS. Hãy xem một ví dụ

import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/") soup = BeautifulSoup(page.content, 'html.parser') # Extract first <h1>(...)</h1> text first_h1 = soup.select('h1')[0].text

import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn.com") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title.text # Extract body of page page_body = soup.body # Extract head of page page_head = soup.head # print the result print(page_body, page_head)0 trả về một danh sách Python gồm tất cả các phần tử. Đây là lý do tại sao bạn chỉ chọn phần tử đầu tiên ở đây với chỉ số import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn.com") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title.text # Extract body of page page_body = soup.body # Extract head of page page_head = soup.head # print the result print(page_body, page_head)2

vượt qua yêu cầu

  • Tạo một biến import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn.com") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title.text # Extract body of page page_body = soup.body # Extract head of page page_head = soup.head # print the result print(page_body, page_head)3. Đặt nó vào danh sách trống
  • Sử dụng import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn.com") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title.text # Extract body of page page_body = soup.body # Extract head of page page_head = soup.head # print the result print(page_body, page_head)0 để chọn tất cả các thẻ import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn.com") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title.text # Extract body of page page_body = soup.body # Extract head of page page_head = soup.head # print the result print(page_body, page_head)5 và lưu trữ văn bản của những thẻ h1 đó trong danh sách import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn.com") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title.text # Extract body of page page_body = soup.body # Extract head of page page_head = soup.head # print the result print(page_body, page_head)3
  • Tạo một biến import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn.com") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title.text # Extract body of page page_body = soup.body # Extract head of page page_head = soup.head # print the result print(page_body, page_head)7 và lưu trữ văn bản của phần tử import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn.com") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title.text # Extract body of page page_body = soup.body # Extract head of page page_head = soup.head # print the result print(page_body, page_head)8 thứ 7 (chỉ số 6) bên trong

Giải pháp cho phòng thí nghiệm này là

import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/") soup = BeautifulSoup(page.content, 'html.parser') # Create all_h1_tags as empty list all_h1_tags = [] # Set all_h1_tags to all h1 tags of the soup for element in soup.select('h1'): all_h1_tags.append(element.text) # Create seventh_p_text and set it to 7th p element text of the page seventh_p_text = soup.select('p')[6].text print(all_h1_tags, seventh_p_text)

Cứ đi đi

Phần 5. Các mặt hàng hàng đầu đang được cạo ngay bây giờ

Đây là liên kết đến phòng thí nghiệm này

Hãy tiếp tục và trích xuất các mục hàng đầu được lấy từ URL. https. //codedamn-lớp học. github. io/webscraper-python-codedamn-classroom-website/

Nếu bạn mở trang này trong một tab mới, bạn sẽ thấy một số mục hàng đầu. Trong phòng thí nghiệm này, nhiệm vụ của bạn là gạch tên của họ và lưu trữ chúng trong một danh sách có tên là import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn.com") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title.text # Extract body of page page_body = soup.body # Extract head of page page_head = soup.head # print the result print(page_body, page_head)9. Bạn cũng sẽ trích xuất các đánh giá cho các mặt hàng này.

Để vượt qua thử thách này, hãy quan tâm đến những điều sau

  • Sử dụng import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn.com") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title.text # Extract body of page page_body = soup.body # Extract head of page page_head = soup.head # print the result print(page_body, page_head)0 để trích xuất tiêu đề. (Gợi ý. một bộ chọn cho tiêu đề sản phẩm có thể là import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title # Extract body of page page_body = soup.body # Extract head of page page_head = soup.head # print the result print(page_title, page_head)1)
  • Sử dụng import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn.com") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title.text # Extract body of page page_body = soup.body # Extract head of page page_head = soup.head # print the result print(page_body, page_head)0 để trích xuất nhãn số lượt đánh giá cho các tiêu đề sản phẩm đó. (Gợi ý. một bộ chọn để đánh giá có thể là import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title # Extract body of page page_body = soup.body # Extract head of page page_head = soup.head # print the result print(page_title, page_head)3) Lưu ý. đây là một nhãn hoàn chỉnh (tôi. e. 2 đánh giá) và không chỉ là một con số
  • Tạo một từ điển mới ở định dạng
info = { "title": 'Asus AsusPro Adv.. '.strip(), "review": '2 reviews\n\n\n'.strip() }
  • Lưu ý rằng bạn đang sử dụng phương pháp import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title # Extract body of page page_body = soup.body # Extract head of page page_head = soup.head # print the result print(page_title, page_head)4 để xóa bất kỳ dòng mới/khoảng trắng bổ sung nào mà bạn có thể có trong đầu ra. Điều này rất quan trọng để vượt qua phòng thí nghiệm này
  • Nối từ điển này vào một danh sách có tên là import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn.com") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title.text # Extract body of page page_body = soup.body # Extract head of page page_head = soup.head # print the result print(page_body, page_head)9
  • In danh sách này ở cuối

Có khá nhiều nhiệm vụ phải hoàn thành trong thử thách này. Trước tiên hãy xem giải pháp và hiểu điều gì đang xảy ra

import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/") soup = BeautifulSoup(page.content, 'html.parser') # Create top_items as empty list top_items = [] # Extract and store in top_items according to instructions on the left products = soup.select('div.thumbnail') for elem in products: title = elem.select('h4 > a.title')[0].text review_label = elem.select('div.ratings')[0].text info = { "title": title.strip(), "review": review_label.strip() } top_items.append(info) print(top_items)

Lưu ý rằng đây chỉ là một trong những giải pháp. Bạn cũng có thể thử điều này theo một cách khác. Trong giải pháp này

  1. Trước hết, bạn chọn tất cả các yếu tố import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title # Extract body of page page_body = soup.body # Extract head of page page_head = soup.head # print the result print(page_title, page_head)6 sẽ cung cấp cho bạn danh sách các sản phẩm riêng lẻ
  2. Sau đó, bạn lặp lại chúng
  3. Bởi vì import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title # Extract body of page page_body = soup.body # Extract head of page page_head = soup.head # print the result print(page_title, page_head)7 cho phép bạn xâu chuỗi chính nó, bạn có thể sử dụng lại select để lấy tiêu đề
  4. Lưu ý rằng vì bạn đang chạy bên trong một vòng lặp cho import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title # Extract body of page page_body = soup.body # Extract head of page page_head = soup.head # print the result print(page_title, page_head)6, nên bộ chọn import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title # Extract body of page page_body = soup.body # Extract head of page page_head = soup.head # print the result print(page_title, page_head)9 sẽ chỉ cung cấp cho bạn một kết quả, bên trong một danh sách. Bạn chọn phần tử thứ 0 của danh sách đó và trích xuất văn bản
  5. Cuối cùng, bạn loại bỏ mọi khoảng trắng thừa và thêm nó vào danh sách của bạn

Đơn giản phải không?

Phần 6. Trích xuất liên kết

Đây là liên kết đến phòng thí nghiệm này

Cho đến giờ bạn đã thấy cách bạn có thể trích xuất văn bản, hay đúng hơn là văn bản bên trong của các phần tử. Bây giờ hãy xem cách bạn có thể trích xuất các thuộc tính bằng cách trích xuất các liên kết từ trang

Đây là một ví dụ về cách trích xuất tất cả thông tin hình ảnh từ trang

import requests # Make a request to //codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/ # Store the result in 'res' variable res = requests.get( '//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/') txt = res.text status = res.status_code print(txt, status) # print the result0

Trong phòng thí nghiệm này, nhiệm vụ của bạn là trích xuất thuộc tính import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/") soup = BeautifulSoup(page.content, 'html.parser') # Extract first <h1>(...)</h1> text first_h1 = soup.select('h1')[0].text0 của các liên kết với cả import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/") soup = BeautifulSoup(page.content, 'html.parser') # Extract first <h1>(...)</h1> text first_h1 = soup.select('h1')[0].text1 của chúng. Đảm bảo những điều sau

  • Bạn phải tạo một danh sách có tên là import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/") soup = BeautifulSoup(page.content, 'html.parser') # Extract first <h1>(...)</h1> text first_h1 = soup.select('h1')[0].text2
  • Trong danh sách này, lưu trữ tất cả thông tin chính tả liên kết. Nó phải ở định dạng sau
import requests # Make a request to //codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/ # Store the result in 'res' variable res = requests.get( '//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/') txt = res.text status = res.status_code print(txt, status) # print the result1
  • Đảm bảo rằng import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/") soup = BeautifulSoup(page.content, 'html.parser') # Extract first <h1>(...)</h1> text first_h1 = soup.select('h1')[0].text1 của bạn không có bất kỳ khoảng trắng nào
  • Đảm bảo rằng bạn đã kiểm tra xem from bs4 import BeautifulSoup page = requests.get("//codedamn.com") soup = BeautifulSoup(page.content, 'html.parser') title = soup.title.text # gets you the text of the <title>(...)</title>7 của mình có phải là Không trước khi bạn gọi import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/") soup = BeautifulSoup(page.content, 'html.parser') # Extract first <h1>(...)</h1> text first_h1 = soup.select('h1')[0].text5 trên đó không
  • Lưu trữ tất cả các lệnh này trong import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/") soup = BeautifulSoup(page.content, 'html.parser') # Extract first <h1>(...)</h1> text first_h1 = soup.select('h1')[0].text2
  • In danh sách này ở cuối

Bạn đang trích xuất các giá trị thuộc tính giống như bạn trích xuất các giá trị từ một lệnh, sử dụng hàm import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/") soup = BeautifulSoup(page.content, 'html.parser') # Extract first <h1>(...)</h1> text first_h1 = soup.select('h1')[0].text7. Hãy xem giải pháp cho phòng thí nghiệm này

import requests # Make a request to //codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/ # Store the result in 'res' variable res = requests.get( '//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/') txt = res.text status = res.status_code print(txt, status) # print the result2

Tại đây, bạn trích xuất thuộc tính import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/") soup = BeautifulSoup(page.content, 'html.parser') # Extract first <h1>(...)</h1> text first_h1 = soup.select('h1')[0].text0 giống như trường hợp hình ảnh. Điều duy nhất bạn đang làm cũng là kiểm tra xem nó có phải là Không có không. Chúng tôi muốn đặt nó thành chuỗi rỗng, nếu không, chúng tôi muốn loại bỏ khoảng trắng

Phần 7. Tạo CSV từ dữ liệu

Đây là liên kết đến phòng thí nghiệm này

Cuối cùng, hãy hiểu cách bạn có thể tạo CSV từ một tập hợp dữ liệu. Bạn sẽ tạo một CSV với các tiêu đề sau

  1. tên sản phẩm
  2. Giá bán
  3. Sự miêu tả
  4. Đánh giá
  5. Hình ảnh sản phẩm

Những sản phẩm này được đặt tại import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/") soup = BeautifulSoup(page.content, 'html.parser') # Extract title of page page_title = soup.title # Extract body of page page_body = soup.body # Extract head of page page_head = soup.head # print the result print(page_title, page_head)6. Bản tóm tắt CSV được cung cấp bên dưới

import requests # Make a request to //codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/ # Store the result in 'res' variable res = requests.get( '//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/') txt = res.text status = res.status_code print(txt, status) # print the result3

Bạn phải trích xuất dữ liệu từ trang web và tạo tệp CSV này cho ba sản phẩm

Yêu cầu vượt qua

  • Tên sản phẩm là phiên bản rút gọn khoảng trắng của tên mặt hàng (ví dụ - Asus AsusPro Adv. )
  • Giá là khoảng trắng được cắt bớt nhưng nhãn giá đầy đủ của sản phẩm (ví dụ - $1101. 83)
  • Mô tả là phiên bản rút gọn khoảng trắng của mô tả sản phẩm (ví dụ - Asus AsusPro Advanced BU401LA-FA271G Dark Grey, 14", Core i5-4210U, 4GB, 128GB SSD, Win7 Pro)
  • Bài đánh giá là phiên bản được cắt bớt khoảng trắng của sản phẩm (ví dụ - 7 bài đánh giá)
  • Hình ảnh sản phẩm là URL (thuộc tính src) của hình ảnh cho một sản phẩm (ví dụ - /webscraper-python-codedamn-classroom-website/cart2. png)
  • Tên của tệp CSV phải là sản phẩm. csv và phải được lưu trữ trong cùng thư mục với tập lệnh của bạn. tập tin py

Hãy xem giải pháp cho phòng thí nghiệm này

import requests # Make a request to //codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/ # Store the result in 'res' variable res = requests.get( '//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/') txt = res.text status = res.status_code print(txt, status) # print the result4

Khối import requests from bs4 import BeautifulSoup # Make a request page = requests.get( "//codedamn-classrooms.github.io/webscraper-python-codedamn-classroom-website/") soup = BeautifulSoup(page.content, 'html.parser') # Create all_h1_tags as empty list all_h1_tags = [] # Set all_h1_tags to all h1 tags of the soup for element in soup.select('h1'): all_h1_tags.append(element.text) # Create seventh_p_text and set it to 7th p element text of the page seventh_p_text = soup.select('p')[6].text print(all_h1_tags, seventh_p_text) 0 thú vị nhất ở đây. Bạn trích xuất tất cả các yếu tố và thuộc tính từ những gì bạn đã học được cho đến nay trong tất cả các phòng thí nghiệm

Khi bạn chạy mã này, bạn sẽ có một tệp CSV đẹp. Và đó là tất cả những điều cơ bản về quét web với BeautifulSoup

Phần kết luận

Tôi hy vọng lớp học tương tác này từ codedamn đã giúp bạn hiểu những kiến ​​thức cơ bản về quét web bằng Python

Nếu bạn thích lớp học này và blog này, hãy cho tôi biết về nó trên twitter và Instagram của tôi. Rất thích nghe phản hồi

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

Mehul Mohan

Nhà phát triển độc lập, người đam mê kỹ thuật bảo mật, thích xây dựng và phá vỡ mọi thứ bằng mã và JavaScript

Chủ đề