Website Scraping with Python: Using BeautifulSoup and Scrapy

所需积分/C币:14 2019-01-15 18:42:03 4.75MB PDF

Website Scraping with Python: Using BeautifulSoup and Scrapy By 作者: Gábor László Hajba ISBN-10 书号: 1484239245 ISBN-13 书号: 9781484239247 Edition 版本: 1st ed. 出版日期: 2018-09-15 pages 页数: 235 Closely examine website scraping and data processing: the technique of extracting data from websites in a format
Website Scraping with Python Gabor laszlo hajba Sopron, Hungary ISBN13(pbk):978-1-48423924-7 ISBN-13( electronic):978-1-4842-39254 htps:// doi. org/10.1007/978-1-484239254 Library of Congress Control Number: 2018957273 Copyright o 2018 by Gabor Laszlo Hajba This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Todd green Development Editor: James Markham Coordinating editor: Jill balzano Cover designed by eStudio Calamar CoverimagedesignedbyFreepik(www.freepik.com) Distributed to the book trade worldwide by Springer Science+ Business Media New York 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax(201)348-4505 e-mailorders-ny@springer-sbm.com,orvisitwww.springeronline.com.ApressMedia,Llcisa California LLC and the sole member (owner) is Springer Science+ Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation Forinformationontranslationspleasee-mailrights@apress.com,orvisitwww.apress.com/ rights- permissions Apress titles may be purchased in bulk for academic, corporate or promotional use. eBook versions and licenses are also available for most titles. for more information reference our print andebooKbulksaleswebpageatwww.apress.com/bulk-sales Any source code or other supplementary material referenced by the author in this book is available toreadersongithubviathebooksproductpage,locatedatwww.apress.com/9781484239247 Formoredetailedinformationpleasevisithttp://www.apress.com/source-code Printed on acid-free paper To those who are restless, like me and always want to learn something new Table of contents About the author ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ About the technical reviewer ■■■■ Acknowledgments XV Introduction Chapter 1: Getting Started mmnaa Website Scraping . Projects for Website Scraping mm. memm..m.....m, 2 Websites are the bottleneck Tools in this book,…3 Preparation Terms and robots Technology of the Website.mentnemnennenmnncnnnnnn, 7 Using chrome Developer Tools 8 Tool Considerations 12 Starting to Code 13 Parsing robots.txt…,,.,,,,,,,,,,,…,…,……13 Creating a Link Extractor.....m...m.mmm....mmm.mmm...mmm...nm..... 15 Extracting Images. Summary…… TABLE OF CONTENTS Chapter 2: Enter the Requirements ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■口■■■■■■■■■■■■■■■■ 19 The Requirements 20 Preparation .m Navigating Through"Meat fish Fish Outlining the Application 31 Navigating the Website. Creating the Navigation.………………33 The requests Library. Switching to requests Putting the Code Together......................... 38 Summary 39 Chapter 3: Using Beautiful Soup mmammmmmmmmmmaan 41 Installing Beautiful Soup.mmmmmnnaaammmen..ammann. 41 Simple Examples .atmnmnnmenaeanannmenaeam.nnn. 42 Parsing html Text 42 Parsing Remote HTML 44 Parsing a file 45 Difference between find and find all Extracting all links 45 Extracting All Images. Finding Tags Through Their Attributes Finding Multiple Tags Based on Property……,,,.,.,,,,,,… Changing Content…… 48 Finding comments..……………52 Converting a soup to htMl Text 53 Extracting the Required Information 53 Identifying, Extracting, and Calling the Target URLS.............54 Navigating the product pages.mmmmmmmmmmmmmmmmmm. 56 TABLE OF CONTENTS Extracting the Informati0n.,………58 Unforeseen Changes....m.m...m. 63 Exporting the Data CSV 66 TO JSON 73 To a relational database,……76 To an NoSQL Database . Performance Improvements Changing the parser Parse only What's Needed. Saving While Working. aaanaacaeaaa nt.88 Developing on a long run 90 Caching Intermediate Step Results..mmanaemmemeananmemmnnann 90 Caching whole Websites 91 Source Code for this chapter 95 Summary.....,…95 Chapter4: Using Scrap,…,…,,,,,,m:,,,::97 Installing scrap mmm.nnn.nt 98 Creating the project 98 Configuring the Project…… ,100 Terminology… 102 Middleware 102 Pipeline 103 Extension 104 Se| ectors…, …104 Implementing the Sainsbury Scraper 106 What's This allowed domains about Preparation 108 TABLE OF CONTENTS def parse(self, response 110 Navigating through categories ..n.taet. 112 Navigating Through the Product Listings.................116 Extracting the Data 118 Where to Put the data Running the Spider.mmm..mmnnannamamamnmmmemennnmmmnnnn 127 Exporting the Results To CSV 134 T0JS0N………………………………………………………135 To Databases 日a Bring Your0 wn Exporter…......…14.3 Caching with Scrapy.......................... 153 Storage Solutions 154 Cache policies…156 Downloading Images. Using Beautiful Soup with Scrapy …161 Logging..,,,,,,,…162 (A Bit) Advanced Configuration 162 LOG LEVEL 163 CONCURRENT REQUESTS 164 DOWNLOAD DELAY mmmmmm, 164 Autothrottling. COOKIES ENABLED, mm, 166 Summary..........167 Chapter 5: Handling JavaScript mmmmmmammmmmmmmmmmmmmmn169 Reverse Engineering…......,,…169 Thoughts on Reverse Engineering……,…,…,,,……172 Summary…...,…………172 TABLE OF CONTENTS Splash 172 Set-up A Dynamic Example Integration with Scrap 177 Adapting the basic Spider....,.,……179 What Happens When Splash Isn't Running?............... 183 Summary 183 Selenium .mou.. 183 Prerequisites…...........,...,184 Basic Usage………… 185 ntegration with Scrapy 186 Summary… 189 Solutions for Beautiful Soup Splash 190 Selenium 191 Summary… 192 Summary Chapter 6: Website Scraping in the cloud ■■■■■■■■■■ ■■■■■■■■■■ ■■■■■■■ 193 Scrap Cloud. Creating a Project 194 Deploying Your Spider.m.mnnmennnennnnn. 195 Start and wait 196 Accessing the Data.mm..mmemanamnnmmannnnmnn 198 AP …200 Limitations mm. 202 Summary… 203 TABLE OF CONTENTS PythonAnywhere .nmmemmnonmnnneeneamnne The Example Script...……203 PythonAnywhere Configuration. Uploading the script 204 Running the script. mnmatnnnneneenennnonnonnn 206 This Works Just Manually 207 Storing Data in a Database? 21 Summary.…., 214 What about Beautiful Soup?.namaaaannmeannnaannma 214 Summary 216 Index 219

...展开详情
img
THESUMMERE

关注 私信 TA的资源

上传资源赚积分,得勋章
相关内容推荐