上周用了一周的时间学习了Python和Scrapy,实现了从0到1完整的网页爬虫实现。研究的时候很痛苦,但是很享受,做技术的嘛。
首先,安装Python,坑太多了,一个个爬。由于我是windows环境,没钱买mac, 在安装的时候遇到各种各样的问题,确实各种各样的依赖。
安装教程不再赘述。如果在安装的过程中遇到 ERROR:需要windows c/c++问题,一般是由于缺少windows开发编译环境,晚上大多数教程是安装一个VisualStudio,太不靠谱了,事实上只要安装一个WindowsSDK就可以了。
下面贴上我的爬虫代码:
爬虫主程序:
# -*- coding: utf-8 -*-
import scrapy
from scrapy.http import Request
from zjf.FsmzItems import FsmzItem
from scrapy.selector import Selector
# 圈圈:情感生活
class MySpider(scrapy.Spider):
#爬虫名
name = "MySpider"
#设定域名
allowed_domains = ["nvsheng.com"]
#爬取地址
start_urls = []
#flag
x = 0
#爬取方法
def parse(self, response):
item = FsmzItem()
sel = Selector(response)
item['title'] = sel.xpath('//h1/text()').extract()
item['text'] = sel.xpath('//*[@class="content"]/p/text()').extract()
item['imags'] = sel.xpath('//div[@id="content"]/p/a/img/@src|//div[@id="content"]/p/img/@src').extract()
if MySpider.x == 0:
page_list = MySpider.getUrl(self,response)
for page_single in page_list: