Skip to content

Commit 305c310

Browse files
committed
Update Web Scraping with Python – Scraping Digital Comics information from Comixology.md
1 parent 4f2a263 commit 305c310

1 file changed

Lines changed: 23 additions & 43 deletions

File tree

‎raw/Web Scraping with Python – Scraping Digital Comics information from Comixology.md‎

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,22 @@
22

33
---
44

5-
## Introduction
5+
## 介绍
66

77
Well, after having talked about the previous analysis that I did about
8-
Comixology in the [previous post in this
9-
series](http://felipegalvao.com.br/blog/2016/05/01/comixology-analise-de-
10-
quadrinhos-digitais-parte-1-analise-do-site-para-web-scraping/), we'll now
8+
Comixology in the [这个系列的前一篇文章](http://felipegalvao.com.br/blog/2016/05/01/comixology-analise-de-quadrinhos-digitais-parte-1-analise-do-site-para-web-scraping/), we'll now
119
talk about the web scraping with Python, the code itself. As we have already
1210
talked about, we will use requests to make requests to web pages and lxml with
1311
Xpath for parsing and information extraction from the desired pages. Let's go.
1412

15-
## Starting to write the code
13+
## 开始写代码
1614

1715
To start the conversation, we will import the necessary libraries, requests
1816
and, from lxml, the html function. To access the HTML parts, we will use
1917
Xpath. So, let's talk about Xpath before we move on to the code. I'll try to
2018
keep it short.
2119

22-
## Xpath: What it is and how to use it
20+
## Xpath:它是啥,以及如何使用它
2321

2422
Xpath is a query language used to extract content from XML / HTML documents.
2523
It can be used to extract information form nodes and attributes, being very
@@ -48,7 +46,7 @@ descendants. Considering the following XML code:
4846
</store>
4947
```
5048

51-
The relations are as following:
49+
其关系如下:
5250

5351
* Father - Elements that contain another elements. In our example, product is the father of class, color and price.
5452
* Children - Analogously, children are elements that are contained in another element. The class, color and price elements are children of the product element.
@@ -194,22 +192,20 @@ something that does not exist in the document, an empty list is returned.
194192
And now that we saw this simple example, let's move on to something more
195193
complex and fun.
196194

197-
## Returning to the code
195+
## 回到代码
198196

199197
Now that we know what Xpath is about, let's use it to extract the Comixology
200198
website itself. For web scraping with Xpath, one of our best friends will be
201199
the "Inspect Element" feature from Chrome or Firefox. It will allow us to see
202200
the source code and the HTML structure and path for a certain element in the
203201
page.
204202

205-
Let's move on to the Publishers section on Comixology. ([click
206-
here](https://www.comixology.com/browse-publisher)). We will extract our code
203+
Let's move on to the Publishers section on Comixology. ([点击这里](https://www.comixology.com/browse-publisher)). We will extract our code
207204
defining the function and creating an empty list which will hold the Publisher
208205
links that we will extract. As we talked on the first post, we will define the
209206
page quantity manually (4 pages, so, we will have a for page in range (1,5)),
210207
and we will pass through each of them. If we go to the site and change to the
211-
next page, we will see that the link becomes: https://www.comixology.com
212-
/browse-publisher?publisherList_pg=2. If we change it back to the first page,
208+
next page, we will see that the link becomes: https://www.comixology.com/browse-publisher?publisherList_pg=2. If we change it back to the first page,
213209
the final part of the link turn back to 1. The first pages of the code will be
214210
as following:
215211

@@ -252,10 +248,7 @@ class "list FeaturedPublisherList", items from the All Publishers table are in
252248
a div that has the class "list publisherList". Let's make our Xpath sentence
253249
going from there:
254250

255-
[![comixology_2_1-publishers](http://felipegalvao.com.br/blog/wp-
256-
content/uploads/2016/05/comixology_2_1-publishers-
257-
1024x925.jpg)](http://felipegalvao.com.br/blog/wp-
258-
content/uploads/2016/05/comixology_2_1-publishers.jpg)
251+
[![comixology_2_1-publishers](http://felipegalvao.com.br/blog/wp-content/uploads/2016/05/comixology_2_1-publishers-1024x925.jpg)](http://felipegalvao.com.br/blog/wp-content/uploads/2016/05/comixology_2_1-publishers.jpg)
259252

260253
Now, I'll create a Xpath string for the extraction of the elements, going from
261254
the div with the class that we just saw (//div[@class="list publisherList"]),
@@ -355,8 +348,7 @@ you will have them on the list returned by the function. First part, concluded
355348
🙂
356349

357350
I'll take this time to talk about a library that will help us in this task,
358-
which is the Pickle package ([to know more about it, click
359-
here](https://wiki.python.org/moin/UsingPickle)). This library will allow us
351+
which is the Pickle package ([想知道更多关于它的信息吗?点击这里](https://wiki.python.org/moin/UsingPickle)). This library will allow us
360352
to export data to files, to load them back later on. In this first moment,
361353
this will not seem very useful, since this extraction function runs very fast
362354
and does not take much time to be completed, because there are only 4 pages to
@@ -385,7 +377,7 @@ passing to it the desired file:
385377
publishers_links = pickle.load(open("publishers_links.p","rb"))
386378
```
387379

388-
## From Publishers to Series
380+
## 从Publishers到Series
389381

390382
Now that we already have the Publishers links, our next step is similar. Now,
391383
we need to define a function that will receive a list of Publisher links, go
@@ -414,28 +406,22 @@ possible cases: A - when the publisher has only one page, B - when there is a
414406
small number of pages (like, from 2 to more or less 10), C - when a publisher
415407
has a great number of pages. See in the image:
416408

417-
[![A - Publisher with 1 page](http://felipegalvao.com.br/blog/wp-
418-
content/uploads/2016/05/comixology_2_2-publisher_no_pages-
419-
300x238.png)](http://felipegalvao.com.br/blog/wp-
420-
content/uploads/2016/05/comixology_2_2-publisher_no_pages.png)
409+
[![A - Publisher with 1 page](http://felipegalvao.com.br/blog/wp-content/uploads/2016/05/comixology_2_2-publisher_no_pages-
410+
300x238.png)](http://felipegalvao.com.br/blog/wp-content/uploads/2016/05/comixology_2_2-publisher_no_pages.png)
421411

422412
A - Publisher with 1 page
423413

424414
|
425415

426-
[![B - Publisher with few pages](http://felipegalvao.com.br/blog/wp-
427-
content/uploads/2016/05/comixology_2_3-publisher_few_pages-
428-
208x300.png)](http://felipegalvao.com.br/blog/wp-
429-
content/uploads/2016/05/comixology_2_3-publisher_few_pages.png)
416+
[![B - Publisher with few pages](http://felipegalvao.com.br/blog/wp-content/uploads/2016/05/comixology_2_3-publisher_few_pages-
417+
208x300.png)](http://felipegalvao.com.br/blog/wp-content/uploads/2016/05/comixology_2_3-publisher_few_pages.png)
430418

431419
B - Publisher with few pages
432420

433421
|
434422

435-
[![C - Publisher with many pages](http://felipegalvao.com.br/blog/wp-
436-
content/uploads/2016/05/comixology_2_4-publisher_many_pages-
437-
228x300.png)](http://felipegalvao.com.br/blog/wp-
438-
content/uploads/2016/05/comixology_2_4-publisher_many_pages.png)
423+
[![C - Publisher with many pages](http://felipegalvao.com.br/blog/wp-content/uploads/2016/05/comixology_2_4-publisher_many_pages-
424+
228x300.png)](http://felipegalvao.com.br/blog/wp-content/uploads/2016/05/comixology_2_4-publisher_many_pages.png)
439425

440426
C - Publisher with many pages
441427

@@ -454,10 +440,8 @@ available, the Publisher has only one page of Series. Simple, isn't it? Well,
454440
let's inspect some elements to see how we are going to build the Xpath
455441
sentence, and then we can go to the code:
456442

457-
[![Path to quantity of Series](http://felipegalvao.com.br/blog/wp-
458-
content/uploads/2016/05/comixology_2_5-series_pages-
459-
1024x469.jpg)](http://felipegalvao.com.br/blog/wp-
460-
content/uploads/2016/05/comixology_2_5-series_pages.jpg)
443+
[![Path to quantity of Series](http://felipegalvao.com.br/blog/wp-content/uploads/2016/05/comixology_2_5-series_pages-
444+
1024x469.jpg)](http://felipegalvao.com.br/blog/wp-content/uploads/2016/05/comixology_2_5-series_pages.jpg)
461445

462446
Path to quantity of Series
463447

@@ -621,7 +605,7 @@ export periodically our data in order to avoid that.
621605
pickle.dump(series_links, open("series_links.p","wb"))
622606
```
623607

624-
## From series to Comics
608+
## 从series到Comics
625609

626610
Now, we have to take one more step to go through Series links and extract
627611
links for the comics itself. Prepare yourselves, cause the execution of this
@@ -663,11 +647,7 @@ perform this extraction code. We will inspect the element to understand the
663647
structure that contains the links that we want, and then, via XPath, we will
664648
see if this structure exists:
665649

666-
[![Comixology - Series - Collected Editions
667-
div](http://felipegalvao.com.br/blog/wp-content/uploads/2016/05
668-
/comixology_2_X-series_collected-
669-
1024x351.jpg)](http://felipegalvao.com.br/blog/wp-content/uploads/2016/05
670-
/comixology_2_X-series_collected.jpg)
650+
[![Comixology - Series - Collected Editions div](http://felipegalvao.com.br/blog/wp-content/uploads/2016/05/comixology_2_X-series_collected-1024x351.jpg)](http://felipegalvao.com.br/blog/wp-content/uploads/2016/05/comixology_2_X-series_collected.jpg)
671651

672652
Comixology - Series - Collected Editions div
673653

@@ -1147,7 +1127,7 @@ And the final part of the function will be like this:
11471127
And finally, to the last step! The extraction of information from each comic
11481128
link.
11491129

1150-
## The last step: extraction of information from comics
1130+
## 最后一步,从comics中抽取信息
11511131

11521132
The last step is relatively simple. We'll have to go into each comic link and
11531133
extract all the information that is possible from these links. One of the
@@ -1478,7 +1458,7 @@ there is a clear element that contains this number. Let's see:
14781458
And so, we complete the information gathering functions. Now, let's set a few
14791459
things so that they all get linked.
14801460

1481-
## Final Touches
1461+
## 最后的润饰
14821462

14831463
We will need three more functions. The first will simply join the comics of
14841464
the links, which are scattered in files, in a single list. The second function

0 commit comments

Comments
 (0)