Python之網絡爬蟲(Xpath語法、Scrapy框架的認識)

一、Xpath語法

xpath是一門在XML文檔中查找信息的語言
1、 節點(Node)

​ 元素、屬性、文本、命名空間、文檔(根)節點

2、 節點關係

​ 父(parent)

​ 子 (Children)

​ 同胞 (Sibling)

​ 先輩 (Ancestor)

​ 後代 (Descendant)

3、 xpath語法

表達式 描述
nodename 選取此節點的所有子節點
// 從任意子節點中選取(第一級)
/ 從根節點選取(下級)
. 選取當前節點(同級)
選取當前節點的父節點(上級)
@ 選取屬性

4、 獲取信息
text():獲取文本信息

[last()-1]:倒數第二個,即最後一個 - 1

@class:選取class屬性

position()<5:選取前4個

5、 解析器比較

解析器 速度 難度
re 最快
BeautifulSoup 非常簡單
lxml 簡單

6、Xpath練習

from lxml import etree
import requests

html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""


selector = etree.HTML(html_doc)
# 取出頁面內所有的鏈接
links = selector.xpath('//p[@class="story"]/a/@href')
for link in links:
    print(link)

# 實例化一個etree選擇器對象
r = requests.get('http://iguye.com/books.xml')
se = etree.HTML(r.text)
print(se.xpath('book'))

print(se.xpath('//book'))
# 選取書店下所有的書本的作者的名字
print(se.xpath('//bookstore/book/author/text()'))
# 選取書店下所有的書本的語言
print(se.xpath('//bookstore/book/title/@lang'))

# 選取書店下第一本書的標題
print(se.xpath('//bookstore/book[1]/title/text()'))
# 選取書店下最後一本書的標題
print(se.xpath('//bookstore/book[last()]/title/text()'))
# 選取書店下倒數第二本書的標題
print(se.xpath('//bookstore/book[last()-1]/title/text()'))
# 選取書店下前2本書的標題
print('前2本書',
      se.xpath('//bookstore/book[position()<3]/title/text()'))
# 選取所有的分類爲web的書本
print('分類爲web的書本', se.xpath('//book[@category="web"]/title/text()'))

# 選取所有價格大於30.00元的書本
print('價格大於30.00元的書本', se.xpath('//book[price>35.00]/price/text()'))

# 選取所有class屬性中包含book的書本的class屬性
print('類名中包含book的書本', se.xpath('//book[contains(@class, "book")]/@class'))

二、Scrapy框架的認識

在掌握了一定的爬蟲基礎之後,我們就可以使用一些框架來快速搭建起我們的爬蟲程序,Scrapy就是一個非常搞笑的爬蟲框架。
1、scrapy引擎工作流程
11
2、名詞解釋
Downloader (下載器):負責下載Scrapy Engine (引擎)發送的所有Requests請求,並將其獲取到的Responses交還給Scrapy Engine(引擎), 由引擎交給Spider來處理。
Spider (爬蟲) :它負責處理所有Responses, 從中分析提取數據,獲取Item字段需要的數據,並將需要跟進的URL提交給引擎,再次進入Scheduler(調度器)。
Item Pipeline(管道):它負責處理Spider中獲取到的Item,並進行進行後期處理(詳細分析、過濾、存儲等)的地方。
Downloader Middlewares (下載中間件):可以當作是一一個可以自定義擴展下載功能的組件。
Spider Mi ddlewares(Spider中間件):可以理解爲是一- 個可以自定擴展和操作引擎和Spider中間通信的功能組件(比如進入Spider的Responses;和從Spider出去的Requests)。

3、製作一個Scrapy爬蟲項目的步驟
1)安裝Scrapy:pip install scrapy

2)創建項目:在終端輸入,scrapy startproject tencentSpider,這裏的項目名爲tencentSpider

3)進入到項目中:cd tencentSpider

4)創建爬蟲:scrapy genspider tencent career.tencent.com
這裏的爬蟲名爲tencent,將來要訪問的域名爲career.tencent.com

5)運行爬蟲:scrapy crawl tencent
這樣一個基本的爬蟲項目就完成了

4、利用Scrapy抓取數據,我們需要修改以下文件
settings.py:設置
註釋了Robots協議;打開了Headers選項並設置了UA;打開了PIPELINES(用來保存文件的)
pipelines.py:保存的邏輯
tencent.py:抓取頁面信息和繼續跳轉的設置
items.py:保存item的映射

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章