scrapy通过css和xpath抓取内容调试

近日使用爬虫scrapy,过程中需要用到对于信息的提取,进行总结,以备后查。

安装scrapy

  • 使用pip install scrapy
  • 注意:过程中有可能存在twist安装出现问题,可以通过百度查找,因为距离当时安装有一段时间了,忘记怎么填坑的了,貌似是通过下载wheel包,手动安装的。

初始化scrapy并创建项目

  • 暂时不总结

测试并进行采集

  • 主要是记录几个规则
  • 抓取页面
    • scrapy shell http://news.nankai.edu.cn/ywsd/index.shtml
  • 抓取内容
    • 抓取css内容中以某些字符开始的href
    • response.css('.cms_block_span a[href^="http://news."]::attr(href)').extract()
    • 抓取xpath内容,xpth可以同chrome浏览器右键得的xpath,直接复制可以。
    • response.xpath('//*[@id=\"root\"]/table[3]/tbody/tr/td[1]/table[2]/tbody/tr[1]/td//text()').extract_first()
    • 抓取xpath内容内部图片的地址。如果是内容,则直接在地址后加//text,如果为img的src,则为//img/@src
    • response.xpath('//*[@id=\"root\"]/table[3]/tbody/tr/td[1]/table[2]/tbody/tr[3]//img/@src').extract()
    • 下面几个有代表意义的参考

备注用到的几个规则记录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
"methods": {
"nankai": {
"info": "南开大学",
"enable": true,
"urls": {
"http://news.nankai.edu.cn/qqxy/index.shtml": true,
"http://news.nankai.edu.cn/nkyw/index.shtml": false,
"http://news.nankai.edu.cn/zhxw/index.shtml": false
},
"crawls": {
"newslist": {
"sel": "css",
"selstr": ".news a::attr(href)",
"seltype": "extract"
},
"title": {
"sel": "css",
"selstr": "title::text",
"seltype": "extract_first"
},
"content": {
"sel": "css",
"selstr": ".news-content",
"seltype": "extract_first"
},
"imgs": {
"sel": "css",
"selstr": ".news-content img::attr(src)",
"seltype": "extract"
},
"tags": [
"南开",
"南开大学",
"nankai.info"
],
"categories": [
"南开大学",
"nankai.info"
]
},
"posts": {
"api_nf": true
}
},
"cumt": {
"info": "中国矿业大学",
"enable": true,
"urls": {
"http://xwzx.cumt.edu.cn/513/list.htm": false,
"http://xwzx.cumt.edu.cn/521/list.htm": false,
"http://xwzx.cumt.edu.cn/523/list.htm": true
},
"crawls": {
"newslist": {
"sel": "css",
"selstr": ".wp_article_list a::attr(href)",
"seltype": "extract"
},
"title": {
"sel": "css",
"selstr": "title::text",
"seltype": "extract_first"
},
"content": {
"sel": "css",
"selstr": ".wp_articlecontent",
"seltype": "extract_first"
},
"imgs": {
"sel": "css",
"selstr": ".wp_articlecontent img::attr(src)",
"seltype": "extract"
},
"tags": [
"矿大",
"中国矿业大学",
"cumt",
"cumt.net.cn"
],
"categories": [
"中国矿业大学",
"cumt.net.cn"
]
},
"posts": [
"api_nf"
]
},
"hrbeu": {
"info": "哈尔滨工程大学",
"enable": true,
"urls": {
"http://gongxue.cn/news/ShowClass.asp?ClassID=4383": false,
"http://gongxue.cn/news/ShowClass.asp?ClassID=4315": true
},
"crawls": {
"newslist": {
"sel": "css",
"selstr": ".wenzhangzhengwen a::attr(href)",
"seltype": "extract"
},
"title": {
"sel": "css",
"selstr": "title::text",
"seltype": "extract_first"
},
"content": {
"sel": "css",
"selstr": ".wenzhangzhengwen",
"seltype": "extract_first"
},
"imgs": {
"sel": "css",
"selstr": ".wenzhangzhengwen img::attr(src)",
"seltype": "extract"
},
"tags": [
"哈工程",
"哈尔滨工程大学",
"hrbeu",
"hrbeu.com.cn"
],
"categories": [
"哈尔滨工程大学",
"hrbeu.com.cn"
]
},
"posts": [
"api_nf"
]
},
"sicau": {
"info": "四川农业大学",
"enable": true,
"urls": {
"https://news.sicau.edu.cn/syzd.htm": true,
"https://news.sicau.edu.cn/syxw.htm": true
},
"crawls": {
"newslist": {
"sel": "css",
"selstr": ".mar_10 a::attr(href)",
"seltype": "extract"
},
"title": {
"sel": "css",
"selstr": "h1::text",
"seltype": "extract_first"
},
"content": {
"sel": "css",
"selstr": ".v_news_content",
"seltype": "extract_first"
},
"imgs": {
"sel": "css",
"selstr": ".v_news_content img::attr(src)",
"seltype": "extract"
},
"tags": [
"川农大",
"四川农业大学",
"sicau",
"sicau.com.cn"
],
"categories": [
"四川农业大学",
"sicau.com.cn"
]
},
"posts": [
"api_nf"
]
},
"utibet": {
"info": "西藏大学",
"enable": false,
"urls": {
"http://www.utibet.edu.cn/news/article_3_5_0.html": true
},
"crawls": {
"newslist": {
"sel": "css",
"selstr": "#right table a::attr(href)",
"seltype": "extract"
},
"title": {
"sel": "xpath",
"selstr": "//*[@id=\"right\"]/div[2]/table/tr[1]/td/div[2]//text()",
"seltype": "extract_first"
},
"content": {
"sel": "xpath",
"selstr": "//*[@id=\"right\"]/div[2]/div",
"seltype": "extract_first"
},
"imgs": {
"sel": "xpath",
"selstr": "//*[@id=\"right\"]/div[2]/div//img/@src",
"seltype": "extract"
},
"tags": [
"藏大",
"西藏大学",
"utibet",
"utibet.com.cn"
],
"categories": [
"西藏大学",
"utibet.com.cn"
],
"posts": [
"api_nf"
]
},
"posts": [
"api_nf"
]
},
"tijmu": {
"info": "天津医科大学",
"enable": true,
"urls": {
"http://www.tmu.edu.cn/132/list.htm": true
},
"crawls": {
"newslist": {
"sel": "css",
"selstr": "#newslist table a::attr(href)",
"seltype": "extract"
},
"title": {
"sel": "css",
"selstr": "title::text",
"seltype": "extract_first"
},
"content": {
"sel": "css",
"selstr": ".Article_Content",
"seltype": "extract_first"
},
"imgs": {
"sel": "css",
"selstr": ".Article_Content img::attr(src)",
"seltype": "extract"
},
"tags": [
"天津医大",
"天津医科大学",
"tijmu",
"tijmu.com.cn",
"tijmu.cc"
],
"categories": [
"天津医科大学",
"tijmu.com.cn",
"tijmu.cc"
]
},
"posts": [
"api_nf"
]
},
"tjutcm": {
"info": "天津中医药大学",
"enable": true,
"urls": {
"http://news13.tjutcm.edu.cn/erji.jsp?urltype=tree.TreeTempUrl&wbtreeid=1526": true
},
"crawls": {
"newslist": {
"sel": "css",
"selstr": "a[class=\"c42389\"]::attr(href)",
"seltype": "extract"
},
"title": {
"sel": "css",
"selstr": ".titlestyle42391::text",
"seltype": "extract_first"
},
"content": {
"sel": "css",
"selstr": "#vsb_newscontent",
"seltype": "extract_first"
},
"imgs": {
"sel": "css",
"selstr": "#vsb_newscontent img::attr(src)",
"seltype": "extract"
},
"tags": [
"天中",
"天津中医药大学",
"tjutcm",
"tjutcm.com.cn"
],
"categories": [
"天津中医药大学",
"tjutcm.com.cn"
]
},
"posts": [
"api_nf"
]
},
"shutcm": {
"info": "上海中医药大学",
"enable": true,
"urls": {
"https://www.shutcm.edu.cn/221/list.htm": true,
"http://www.shutcm.edu.cn/xykx/list.htm": true
},
"crawls": {
"newslist": {
"sel": "css",
"selstr": ".column-news-title a::attr(href)",
"seltype": "extract"
},
"title": {
"sel": "css",
"selstr": "title::text",
"seltype": "extract_first"
},
"content": {
"sel": "css",
"selstr": ".wp_articlecontent",
"seltype": "extract_first"
},
"imgs": {
"sel": "css",
"selstr": ".wp_articlecontent img::attr(src)",
"seltype": "extract"
},
"tags": [
"上中医",
"上海中医药大学",
"shutcm",
"shutcm.com.cn"
],
"categories": [
"上海中医药大学",
"shutcm.com.cn"
]
},
"posts": [
"api_nf"
]
},
"nwsuaf": {
"info": "西北农林科技大学",
"enable": true,
"urls": {
"https://news.nwsuaf.edu.cn/xnxw/index.htm": true
},
"crawls": {
"newslist": {
"sel": "css",
"selstr": ".lb01 a::attr(href)",
"seltype": "extract"
},
"title": {
"sel": "css",
"selstr": "title::text",
"seltype": "extract_first"
},
"content": {
"sel": "css",
"selstr": "#work",
"seltype": "extract_first"
},
"imgs": {
"sel": "css",
"selstr": "#work img::attr(src)",
"seltype": "extract"
},
"tags": [
"西农",
"西北农林",
"西北农林科技大学",
"nwsuaf",
"nwsuaf.cc"
],
"categories": [
"西北农林科技大学",
"nwsuaf.cc"
]
},
"posts": [
"api_nf"
]
},
"shsmu": {
"info": "上海交通大学医学院",
"enable": true,
"urls": {
"https://www.shsmu.edu.cn/news/xyyw.htm": true
},
"crawls": {
"newslist": {
"sel": "css",
"selstr": "#s65872874_content table a::attr(href)",
"seltype": "extract"
},
"title": {
"sel": "css",
"selstr": ".mod_font08.mod_bold.mod_align::text",
"seltype": "extract_first"
},
"content": {
"sel": "css",
"selstr": "#article_content",
"seltype": "extract_first"
},
"imgs": {
"sel": "css",
"selstr": "#article_content img::attr(src)",
"seltype": "extract"
},
"tags": [
"上海交大医学院",
"上海交通大学医学院",
"shsmu",
"shsmu.cc"
],
"categories": [
"上海交通大学医学院",
"shsmu.cc"
]
},
"posts": [
"api_nf"
]
},
"swjtu": {
"info": "西南交通大学",
"enable": true,
"urls": {
"https://news.swjtu.edu.cn/showlist-82.shtml": true
},
"crawls": {
"newslist": {
"sel": "css",
"selstr": ".detail__list__1.body__left__list a::attr(href)",
"seltype": "extract"
},
"title": {
"sel": "css",
"selstr": ".article h1::text",
"seltype": "extract_first"
},
"content": {
"sel": "css",
"selstr": ".article .content14",
"seltype": "extract_first"
},
"imgs": {
"sel": "css",
"selstr": ".article .content14 img::attr(src)",
"seltype": "extract"
},
"tags": [
"西南交大",
"西南交通大学",
"swjtu",
"swjtu.cc"
],
"categories": [
"西南交通大学",
"swjtu.cc"
]
},
"posts": [
"api_nf"
]
},
"njust": {
"info": "南京理工大学",
"enable": true,
"urls": {
"http://zs.njust.edu.cn/zhywx/list.htm": true
},
"crawls": {
"newslist": {
"sel": "css",
"selstr": ".wp_article_list a::attr(href)",
"seltype": "extract"
},
"title": {
"sel": "css",
"selstr": ".arti_title::text",
"seltype": "extract_first"
},
"content": {
"sel": "css",
"selstr": ".wp_articlecontent",
"seltype": "extract_first"
},
"imgs": {
"sel": "css",
"selstr": ".wp_articlecontent img::attr(src)",
"seltype": "extract"
},
"tags": [
"南理工",
"南京理工大学",
"njust",
"njust.cc"
],
"categories": [
"南京理工大学",
"njust.cc"
]
},
"posts": [
"api_nf"
]
},
"sufe": {
"info": "上海财经大学",
"enable": true,
"urls": {
"http://news.sufe.edu.cn/179/list.htm": true
},
"crawls": {
"newslist": {
"sel": "css",
"selstr": "#wp_news_w69 a::attr(href)",
"seltype": "extract"
},
"title": {
"sel": "css",
"selstr": ".arti-title::text",
"seltype": "extract_first"
},
"content": {
"sel": "css",
"selstr": ".wp_articlecontent",
"seltype": "extract_first"
},
"imgs": {
"sel": "css",
"selstr": ".wp_articlecontent img::attr(src)",
"seltype": "extract"
},
"tags": [
"上财",
"上海财经大学",
"sufe",
"sufe.cc"
],
"categories": [
"上海财经大学",
"sufe.cc"
]
},
"posts": [
"api_nf"
]
}
}