在報(bào)告前面奢讨,先放一些困擾我很久的debug 讓我終生銘記的debug
就是這個(gè)所謂的unexpected indent 讓我刪了一次又一次的new.py 重新改又重新傳輸上去開(kāi)始爬 一遍又一遍的在百度和同學(xué)之間詢問(wèn)求解 著實(shí)令我難忘稚叹!從縮進(jìn)用tab和空格開(kāi)始改 然后改到每一個(gè)符號(hào)(,:)最后改到行之間是否需要空行 拿诸。這一個(gè)問(wèn)題周周轉(zhuǎn)轉(zhuǎn)得有個(gè)幾十次循環(huán)扒袖。
然后是invalid syntax 又是一個(gè)格式方面的問(wèn)題 和前面差不多 改的方式都差不多 就不多說(shuō) 反正也很不容易啊亩码!
然后就是比較核心的問(wèn)題了 在spider的上層目錄里沒(méi)有改好items文件 導(dǎo)致后面不斷出現(xiàn)路徑上的錯(cuò)誤 后來(lái)直接新建了一個(gè)工程 一步一步往目錄里面走 重寫(xiě)了item.py 然后參考了郭盛鑲同學(xué)的報(bào)告 將starturl改正為http://ggglxy.scu.edu.cn/index.phpc=article&a=type&tid=18&page_1_page=1
避免了爬不到數(shù)據(jù)的情況季率。
以上為修改后的item文件
import scrapy
from ggteachers.items import GgteachersItem
class TeachersSpider(scrapy.Spider):
name = "teachers"
start_urls = [
'http://ggglxy.scu.edu.cn/index.php?c=article&a=type&tid=18&page_1_page=1',
]
def parse(self, response):
for href in response.css('div.right_info.p20 ul.teachers_ul.mt20.cf li.fl div.l.fl a::attr(href)'):
url = response.urljoin(href.extract())
yield scrapy.Request(url, callback=self.parse1)
next_page = response.css('div.w780.pb30.mb30.fr div.right_info.p20 div.pager.cf.tc.pt10.pb10.mobile_dn li.c::text').extract_first()
if next_page is not None:
next_url = int(next_page) + 1
next_urls = '?c=article&a=type&tid=18&page%s' % next_url
print next_urls
next_urls = response.urljoin(next_urls)
yield scrapy.Request(next_urls,callback = self.parse)
def parse1(self, response):
items = []
for second in response.css('div.js_infobox.cf'):
item = GgteachersItem()
item['names'] = second.css('div.r.fr h3::text').extract_first(),
item['career'] = second.css('div.r.fr p::text').extract_first(),
item['intro'] =second.css('div.desc::text').extract_first(),
items.append(item)
return items
以上為spider代碼
然后開(kāi)始執(zhí)行
然后是爬取數(shù)據(jù)的部分截圖
![D~`A_H4Y29QUTZ(]LWA8Q)A.png](http://upload-images.jianshu.io/upload_images/5886630-4e14a6e308d7c8dd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
然后出現(xiàn)了與郭盛鑲同學(xué)一樣的問(wèn)題 參考了他的解決方法后 下載保存
scrapy crawl teachers -o teachers.json
sz teachers.json
然后就succeed了
顯然 是我高興的太早了 只爬到了少量的老師簡(jiǎn)介 明天上午再看吧 要斷網(wǎng)了
以上