Sẵn sàng thu thập dữ liệu

Web Scraping với Python

Thomas Laetsch

Data Scientist, NYU

Hãy dùng Response

Selector vs Response:

  • Response có đủ công cụ đã học với Selector:
    • Phương thức xpath, css kèm theo extractextract_first.
  • Response cũng lưu lại URL nguồn nơi tải HTML.
  • Response giúp chuyển từ trang này sang trang khác để "thu thập" trên web.
Web Scraping với Python

Những điều đã biết

  • xpath hoạt động như một Selector
response.xpath( '//div/span[@class="bio"]' )
  • css hoạt động như một Selector
response.css( 'div > span.bio' )
  • Chuỗi phương thức hoạt động như một Selector
response.xpath('//div').css('span.bio')
  • Trích xuất dữ liệu hoạt động như một Selector
response.xpath('//div').css('span.bio').extract()
response.xpath('//div').css('span.bio').extract_first()
Web Scraping với Python

Những điều chưa biết

  • response lưu URL trong biến response.url.
response.url
>>> 'http://www.DataCamp.com/courses/all'
  • response cho phép "theo" liên kết mới với phương thức follow()
# next_url là chuỗi đường dẫn của URL kế tiếp cần thu thập
response.follow( next_url )
  • Sẽ học thêm về follow sau.
Web Scraping với Python

Trong Response

Web Scraping với Python

Preparing Video For Download...