在我们处理完数据后,习惯把它放在原有的位置,但是这样也会出现一定的隐患。如果因为新数据的加入或者其他种种原因,当我们再次想要启用这个文件的时候,小伙伴们就会开始着急却怎么也翻不出来,似乎也没有其他更好的搜集办法,而重新进行数据整理显然是不现实的。下面我们就一起看看python爬虫中scrapy处理项目数据的方法吧。

1、拉取项目

$ git clone https://github.com/jonbakerfish/tweetscraper.git

$ cd tweetscraper/

$ pip install -r requirements.txt #add '--user' if you are not root

$ scrapy list

$ #if the output is 'tweetscraper', then you are ready to go.

2、数据持久化

通过阅读文档,我们发现该项目有三种持久化数据的方式,第一种是保存在文件中,第二种是保存在mongo中,第三种是保存在mysql数据库中。因为我们抓取的数据需要做后期的分析,所以,需要将数据保存在mysql中。

抓取到的数据默认是以json格式保存在磁盘 ./data/tweet/ 中的,所以,需要修改配置文件 tweetscraper/settings.py 。

item_pipelines = {  # 'tweetscraper.pipelines.savetofilepipeline':100,
#'tweetscraper.pipelines.savetomongopipeline':100, # replace `savetofilepipeline` with this to use mongodb
  'tweetscraper.pipelines.savetomysqlpipeline':100, # replace `savetofilepipeline` with this to use mysql
}
#settings for mysql
mysql_server = "18.126.219.16"
mysql_db   = "scraper"
mysql_table = "tweets" # the table will be created automatically
mysql_user  = "root"    # mysql user to use (should have insert access granted to the database/table
mysql_pwd  = "admin123456"    # mysql user's password

内容扩展:

scrapy.cfg是项目的配置文件

from scrapy.spider import basespider
class dmozspider(basespider):
name = "dmoz"
allowed_domains = ["dmoz.org"]
start_urls = [
  "http://www.dmoz.org/computers/programming/languages/python/books/",
  "http://www.dmoz.org/computers/programming/languages/python/resources/"
]
def parse(self, response):
  filename = response.url.split("/")[-2]
  open(filename, 'wb').write(response.body)

到此这篇关于python中scrapy处理项目数据的实例分析的文章就介绍到这了,更多相关python爬虫中scrapy如何处理项目数据内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!