C 3. 背诵 - scrapy爬虫常用页面解析测试方法

scrapy爬虫页面解析测试工具


描述:编写页面解析代码时,需要预先执行代码,查找错误,有两种方法

  • 方法一: scrapy shell < URL > 命令
# 运行完这条命令后,页面信息会打包放在 response 中
scrapy shell http://books.com
  • 方法二: 通过 requests 构造 response
from scrapy.selector import Selector
from scrapy.http import HtmlResponse
import requests

body = requests.get('http://bang.dangdang.com/books/bestsellers/01.00.00.00.00.00-month-2018-6-1-1').text
response = HtmlResponse(url='http://example.com', body=body, encoding='utf-8')

name = response.xpath('//div[@class="name"]//@title').extract_first()

猜你喜欢

转载自blog.csdn.net/qq_41682050/article/details/81148522