Response對象的幾點說明:
Response對象用來描述一個HTTP響應,Response只是一個基類,根據相應的不同有如下子類:
TextResponse,HtmlResponse,XmlResponse
僅以HtmlResponse為例,HtmlResponse在基類Response的基礎上,還多了很多新的方法。
一.使用Selector
因為鏈接也是頁面中的數據,所以可以使用與提取數據相同的方法進行提取。在分析網頁時可以通過jupyter notebook構造selector對象進行分析(selector對象有xpath和css方法)
import requests
from scrapy.selector import Selector
res=requests.get("http://books.toscrape.com/catalogue/a-light-in-the-attic_1000/index.html")
selector=Selector(response=res)
二 .使用 scrapy框架中的linkextractors模塊
用法見相關資料
1. le.extractor_links(response)中的response指的是HtmlResponse
2.HtmlResponse的構造方法:
from scrapy.http import HtmlResponse
from scrapy.linkextractors import LinkExtractor
import requests
#先構造Response對象,再用Response對象構造HtmlResponse對象,從而能夠使用linkextractor模塊
ResStack=requests.get("http://books.toscrape.com/catalogue/a-light-in-the-attic_1000/index.html")
res = HtmlResponse(url="http://books.toscrape.com/catalogue/a-light-in-the-attic_1000/index.html" , body=ResStack.text , encoding="utf-8")
注:1.HtmlResponse包含多種參數,具體如何使用可查書
2.HtmlResponse也包含多種方法,比如css,xpath,text等方法,也可以通過jupyter notebook進行網頁分析,而且也可以使用linkextractor提取鏈接,更加方便