Climb static website is divided into two parts:
- Static text crawl websites
- Pictures climb static websites
[TOC]
Text climb
Thinking
- With
requests
get HTML website module - We used
BeautifulSoup
to give regular HTML text module - Use
find
orfind_all
function of getting what you want from a regular text - By
repalce
removing unwanted characters
Source
1 |
|
We can see the results
Climb pictures
Thinking
- Get HTML sites with requests module
- Get regular HTML text with BeautifulSoup
- You get what you want from the regular text with a find function, such as keyword img
- Use of urllib download
- Download photos for use statement
Source
. 1 |
|
爬虫时必须会用网页源代码
以爬图片为例:
这是我们要爬的网站:[https://darerd.github.io/2019/03/21/随想-新零售企业““智胜”未来/]
打开网站后(我用的Chrome浏览器),键盘快捷键F12,即可打开网站的调试模式,效果如下:
右侧就是网站的源代码,可以用来爬
如果需要快速定位到某一部分的代码所在位置,我们可以鼠标右键,选择检查,如下图所示:
如果我们要快速定位某图片所在的代码位置,演示如下:
用这种方法观察每一张图片的源码:
它们的写法都是非常类似的,如下:
1 |
<img src="http://upload.hbrchina.org/2019/0213/1550028457604.jpg" alt="1550028961(1)"> |
src
是图片的下载地址,alt
是图片的便签,每一张图片都在img
语句中
所以我们只要得到所有的img
语句,然后从img
语句中得到所有的src
链接,就可以下载图片了。
每一种爬虫程序都类似,找到要爬部分的特点,然后调用相应的模块。
对于小白,难度就在于怎么样找到要爬部分的特点
以上