1.在Pycharm的Terminal中输入“scrapy startproject news”创建爬虫项目,“news”为项目名。
2.自动生成的工程目录
3.编写item.py,也就是定义要爬取信息的字段
4.进入news/news/spiders目录下,使用命令“ scrapy genspider -t crawl newscrawl ‘news.sina.com.cn’ ”创建爬虫名为“newscrawl”的爬虫文件,爬虫域是“news.sina.com.cn”。
5.编写newscrawl.py文件。其中“https://news.sina.com.cn/\w/time/\w+-\w+.shtml”正则是用来匹配“http://news.sina.com.cn/hotnews/”页面上的链接的。注:可以查看链接的规律个性化定制。
xpath的匹配可以通过“xpath helper”插件来快速定位,或者通过chrome浏览器按F1