Best way for a beginner to learn screen scraping by Python

Question:

This might be one of those questions that are difficult to answer, but here goes:

I don’t consider my self programmer – but I would like to 🙂 I’ve learned R, because I was sick and tired of spss, and because a friend introduced me to the language – so I am not a complete stranger to programming logic.

Now I would like to learn python – primarily to do screen scraping and text analysis, but also for writing webapps with Pylons or Django.

So: How should I go about learning to screen scrape with python? I started going through the scrappy docs but I feel to much “magic” is going on – after all – I am trying to learn, not just do.

On the other hand: There is no reason to reinvent the wheel, and if Scrapy is to screen scraping what Django is to webpages, then It might after all be worth jumping straight into Scrapy. What do you think?

Oh – BTW: The kind of screen scraping: I want to scrape newspaper sites (i.e. fairly complex and big) for mentions of politicians etc. – That means I will need to scrape daily, incrementally and recursively – and I need to log the results into a database of sorts – which lead me to a bonus question: Everybody is talking about nonSQL DB. Should I learn to use e.g. mongoDB right away (I don’t think I need strong consistency), or is that foolish for what I want to do?

Thank you for any thoughts – and I apologize if this is to general to be considered a programming question.

Asked By: Andreas

||

Answers:

Looks like Scrappy is using XPATH for DOM traversal, which is a language itself and may feel somewhat cryptic for some time. I think BeautifulSoup will give you a faster start. With lxml you’ll have to invest more time learning, but it generally considered (not only by me) a better alternative to BeautifulSoup.

For database I would suggest you to start with SQLite and use it until you hit a wall and need something more scalable (which may never happen, depending on how far you want to go with that), at which point you’ll know what kind of storage you need. Mongodb is definitely overkill at this point, but getting comfortable with SQL is a very useful skill.

Here is a five-line example I gave some time ago to illustrate hoe BeautifulSoup can be used.
Which is the best programming language to write a web bot?

Answered By: cababunga

Per the database part of the question, use the right tool for the job. Figure out what you wanna do, how you wanna organize your data, what kind of access you need, etc. THEN decide if a no-sql solution works for your project.

I think no-sql solutions are here to stay for a variety of different applications. We’ve implemented them on various projects I’ve worked on in the last 20 years inside of SQL databases without dubbing it no-sql so the applications exist. So it’s worth at least getting some background on what they offer and which products are working well to date.

Design your project well, and keep the persistence layer separate, and you should be able to change your database solution with only minor heartache if you decide that’s what’s necessary.

Answered By: Marvo

I agree that the Scrapy docs give off that impression. But, I believe, as I found for myself, that if you are patient with Scrapy, and go through the tutorials first, and then bury yourself into the rest of the documentation, you will not only start to understand the different parts to Scrapy better, but you will appreciate why it does what it does the way it does it. It is a framework for writing spiders and screen scrappers in the real sense of a framework. You will still have to learn XPath, but I find that it is best to learn it regardless. After all, you do intend to scrape websites, and an understanding of what XPath is and how it works is only going to make things easier for you.

Once you have, for example, understood the concept of pipelines in Scrapy, you will be able to appreciate how easy it is to do all sorts of stuff with scrapped items, including storing them into a database.

BeautifulSoup is a wonderful Python library that can be used to scrape websites. But, in contrast to Scrapy, it is not a framework by any means. For smaller projects where you don’t have to invest time in writing a proper spider and have to deal with scraping a good amount of data, you can get by with BeautifulSoup. But for anything else, you will only begin to appreciate the sort of things Scrapy provides.

Answered By: ayaz

I recommend starting lower level while learning – scrapy is a high level framework.
Read a good Python book like Dive Into Python then look at lxml for parsing HTML.

Answered By: hoju

I really like BeautifulSoup. I’m fairly new to Python but found it fairly easy to start screen scraping. I wrote a brief tutorial on screen scraping with beautiful soup. I hope it helps.

Answered By: Omer Khan

before diving into Scrapy take Udacity’s introduction to Computer Science: https://www.udacity.com/course/cs101

That’s a great way to familiarize yourself with Python and you will actually learn Scrapy lot faster once you have some basic knowledge of Python.

Answered By: Jaakko