先用CSS提取数据学习章节方法定位文章位置:
图中红色箭头属性为全局唯一
下面图片为要抓取的文章对应图片样例:
图中红色箭头为要抓取图片
下面是对应的html源码
<dl class="list_userbar">
<dt>
<a href="https://blog.csdn.net/valada" target="_blank" class="user_img">
<img src="https://avatar.csdn.net/E/2/6/1_valada.jpg" alt="valada" title="valada">
</a>
</dt>
<dd class="name">
<a href="https://blog.csdn.net/valada?utm_source=feed" target="_blank">
GitChat的博客 </a>
</dd>
<div class="interval"></div>
<dd class="tag">
<a href="/nav/ai" target="_blank">
人工智能 </a>
</dd>
<div class="interval"></div>
<dd class="time">
08月14日 </dd>
<div class="interval"></div>
<dd class="blogtitle">
来自 <a href="https://blog.csdn.net/valada?utm_source=feed" target="_blank">GitChat 的 blog</a>
</dd>
<div class="interactive floatR">
<!--阅读 begin-->
<dd class="read_num">
<a href="https://blog.csdn.net/valada/article/details/81639661" data-track-click="{"mod":"popu_459","con":",https://blog.csdn.net/valada/article/details/81639661,none"}">
<span class="num">3845</span>
<span class="text">阅读</span>
</a>
</dd>
<!--阅读 end-->
<!--新增评论数+阅读 begin-->
<div class="interval"></div>
<dd class="common_num ">
<a href="https://blog.csdn.net/valada/article/details/81639661#commentBox" target="_blank" data-track-click="{"mod":"popu_459","con":",https://blog.csdn.net/valada/article/details/81639661#commentBox,none"}">
<span class="num">10</span> <span class="text">评论</span>
</a>
</dd>
<!--新增评论数+阅读 end-->
</div>
</dl>
html源码分析:从代码中可以看到img对应的src地址是直接可访问地址,直接获取地址后就能获取图片。
下面是应用scrapy抓取代码:
#use scrapy shell debug
scrapy shell https://blog.csdn.net/
#get article title and url
response.css("#feedlist_id .clearfix .list_con .title h2 a::text").extract()
response.css("#feedlist_id .clearfix .list_con .title h2 a::attr(href)").extract()
#get the portrait of the author
import scrapy
import re
from scrapy.http import Request
from urllib import parse
post_nodes = response.css("#feedlist_id .clearfix .list_con .list_userbar dt a")
for post_node in post_nodes:
image_url = post_node.css("img::attr(src)").extract_first("")
post_url = post_node.css("::attr(href)").extract_first("")
yield Request(url=parse.urljoin(response.url,post_url), meta={"front_image_url":}, callback=self.parse_detail)
在运行时会发现缺少pillow组件需安装,安装如下:
(py3env) MacBook~/Python/tutorial/MySpider$ pip3 install -i https://pypi.douban.com/simple pillow
Looking in indexes: https://pypi.douban.com/simple
Collecting pillow
Downloading https://pypi.doubanio.com/packages/d1/21/bef2816809fac16754e07ed935469fc65f42ced1a94766de7c804179311d/Pillow-5.3.0-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (3.6MB)
100% |████████████████████████████████| 3.6MB 2.2MB/s
Installing collected packages: pillow
Successfully installed pillow-5.3.0
You are using pip version 18.0, however version 18.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
网友评论