一、打开PythonCharm
打开terminal:alt+f12或者view→tool windows→terminal
cd进入指定文件位置
#scrapy startproject +项目名称
scrapy startproject wenzhang
里面有一个wenzhang文件夹还有一个scrapy.cfg文件(config)
init.py 导包需要用到
item.py
middlewares.py
pipelines.py
settings.py
terminal进入spiders
scrapy genspider wenzhang itcast.cn
在wenzhang.py中,注释pass后添加print(respond.body)
terminal中输入scrapy crawl wenzhang
即可获得数据
深入学习:
wenzhang.py中:
name=后面是爬虫名
allowed_domains是爬取范围在此域名下1.3版本是元组()中间用“,”分割 而现在都改为了列表[ ]
item——
1.定义结构化字段,用来保护爬取到的数据
qiushibaike.py中:
def parse(self, response):
with open("shuju.html","w",encoding = "utf-8") as f:
f.write(response.text)
————————————————————————————————
XPath表达式的例子以及对应含义
/html/head/title:选择文档中标签内的