马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 Stubborn 于 2020-1-7 22:51 编辑
Scrapy的实际案例01(普通爬虫)-豆瓣Top250
豆瓣top250 ->https://movie.douban.com/top250?start=0
首先,建立好爬虫项目,新建一个scrapy爬虫(普通爬虫<scrapy genspider Douban douban.com>),定义好需要采集的信息:
items.py文件
下面开始编写爬虫文件了。
爬虫文件编辑好之后,嗯,数据到管道(Pinline)了,这里只是做简单的示范,就不存数据库里面去了
class DoubanPipeline(object):
def process_item(self, item, spider):
String = f"{item['name'],item['comment'],item['score'],item['assess'],item['actor']}\n"
with open("douban.csv", "a", encoding="utf-8") as f:
f.write(String)
return item
最后记得在setting里面设置一些东西:# Crawl responsibly by identifying yourself (and your website) on the user-agent
# 设置请求头
USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36'
# Obey robots.txt rules
# 不遵循协议,遵循啥都没有,QAQ
ROBOTSTXT_OBEY = False
# 开启定义好的数据管道
ITEM_PIPELINES = {
'ScrapyMode.pipelines.DoubanPipeline': 300,
}
最后关于运行。你可以选择在cmd 上进入到 项目目录里面,使用:scrapy crawl Douban # 豆瓣是我的爬虫名字,即DoubanSpider里面的name属性
最后教一个黑科技。可以写代码运行Scrapy爬虫:
|