質問編集履歴
5
ソースコードの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -102,7 +102,71 @@
|
|
102
102
|
|
103
103
|
サンプルコードののfor url in response.scc('a.entry-link::attr("href")')をfor url in response.css('.entrylist-contents-title a::attr("herf")')やfor url in response.css('a.entrylist-contents-title::attr("herf")'),for url in response.css('.entrylist-contents-title a::attr("herf")')に書き換えてみたりしましたが動きませんでした。
|
104
104
|
|
105
|
+
```python
|
105
106
|
|
107
|
+
# -*- coding: utf-8 -*-
|
108
|
+
|
109
|
+
import scrapy
|
110
|
+
|
111
|
+
from myproject.items import Page
|
112
|
+
|
113
|
+
from myproject.utils import get_content
|
114
|
+
|
115
|
+
from bs4 import BeautifulSoup
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
class BroadSpider(scrapy.Spider):
|
120
|
+
|
121
|
+
name = 'broad'
|
122
|
+
|
123
|
+
allowed_domains = ['b.hatena.ne.jp/entrylist']
|
124
|
+
|
125
|
+
start_urls = ['http://b.hatena.ne.jp/entrylist/']
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
def parse(self, response):
|
130
|
+
|
131
|
+
print('\n\nresponse:{}\n\n'.format(response))
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
for url in response.css('.entrylist-contents-title a::attr("href")').extract():
|
136
|
+
|
137
|
+
print('\n\nURL:{}'.format(url))
|
138
|
+
|
139
|
+
yield scrapy.Request(url,callback=self.parse_page)
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
url_more=response.css('a::attr("href")').re_first(r'.*?of=\d{2}$')
|
146
|
+
|
147
|
+
print('\n\nurl_more:{}\n\n'.format(url_more))
|
148
|
+
|
149
|
+
if url_more:
|
150
|
+
|
151
|
+
yield scrapy.Request(responce.urljoin(url_more))
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
def parse_page(self, response):
|
156
|
+
|
157
|
+
print('\n\npase_page\n\n')
|
158
|
+
|
159
|
+
title, content = get_content(reaponse.text)
|
160
|
+
|
161
|
+
yield Page(url=responce.url, title=title , content=content)
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
```
|
168
|
+
|
169
|
+
に修正したところ最初のfor文は動きました。urlmoreには相変わらず何も入っていません。
|
106
170
|
|
107
171
|
### 補足情報(FW/ツールのバージョンなど)
|
108
172
|
|
4
タグの編集
test
CHANGED
File without changes
|
test
CHANGED
File without changes
|
3
詳細の追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,9 +2,13 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
-
python クローリング&スクレイピングーデータ収集・解析のための実践開発ガイドーというテキストのp223のreadabiltyを利用するSpider
|
5
|
+
python クローリング&スクレイピングーデータ収集・解析のための実践開発ガイドーというテキストのp223のreadabiltyを利用するSpiderの実装に詰まっています。個別ページをたどる関数(pase)と個別webページをパースする関数(pase_page)が動かない状態です。
|
6
6
|
|
7
7
|
エントリーページ内のURL取得してfor文を回したいのですがどう書いたらいいのかわかりません。
|
8
|
+
|
9
|
+
paseメソッドの一つ目のfor文ではh3タグ内にあるクラスentrylist-contents-title内のurlがあればfor文をまわすという風にしたいです。
|
10
|
+
|
11
|
+
二つ目ではof=の値が2桁である場合のみ次の20件をたどるという風にしたいです。
|
8
12
|
|
9
13
|
ご教授お願い致します。
|
10
14
|
|
@@ -14,9 +18,9 @@
|
|
14
18
|
|
15
19
|
|
16
20
|
|
17
|
-
```
|
21
|
+
```
|
18
22
|
|
19
|
-
|
23
|
+
INFO: Enabled spider middlewares:
|
20
24
|
|
21
25
|
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
|
22
26
|
|
@@ -28,17 +32,23 @@
|
|
28
32
|
|
29
33
|
'scrapy.spidermiddlewares.depth.DepthMiddleware']
|
30
34
|
|
35
|
+
|
36
|
+
|
31
37
|
```
|
32
38
|
|
33
39
|
|
34
40
|
|
35
41
|
### python
|
36
42
|
|
43
|
+
# -*- coding: utf-8 -*-
|
44
|
+
|
37
45
|
import scrapy
|
38
46
|
|
39
47
|
from myproject.items import Page
|
40
48
|
|
41
49
|
from myproject.utils import get_content
|
50
|
+
|
51
|
+
from bs4 import BeautifulSoup
|
42
52
|
|
43
53
|
|
44
54
|
|
@@ -48,31 +58,39 @@
|
|
48
58
|
|
49
59
|
allowed_domains = ['b.hatena.ne.jp/entrylist']
|
50
60
|
|
51
|
-
start_urls = ['http://b.hatena.ne.jp/entrylist/']
|
61
|
+
start_urls = ['http://b.hatena.ne.jp/entrylist/']
|
62
|
+
|
63
|
+
|
52
64
|
|
53
65
|
def parse(self, response):
|
54
66
|
|
67
|
+
|
68
|
+
|
55
|
-
for url in response.css('
|
69
|
+
for url in response.css('.entrylist-contents-title a::attr("herf")').extract():
|
56
70
|
|
57
71
|
yield scrapy.Request(url,callback=self.parse_page)
|
58
72
|
|
59
73
|
|
60
74
|
|
75
|
+
|
76
|
+
|
61
77
|
url_more=response.css('a::attr("href")').re_first(r'.*?of=\d{2}$')
|
62
|
-
|
63
|
-
print("url_more:{}".format(url_more))
|
64
78
|
|
65
79
|
if url_more:
|
66
80
|
|
67
81
|
yield scrapy.Request(responce.urljoin(url_more))
|
68
82
|
|
69
|
-
|
83
|
+
|
70
84
|
|
71
85
|
def parse_page(self, response):
|
72
86
|
|
73
87
|
title, content = get_content(reaponse.text)
|
74
88
|
|
75
|
-
yield Page(url=responce.url, title=title , content=content)
|
89
|
+
yield Page(url=responce.url, title=title , content=content)
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
```
|
76
94
|
|
77
95
|
|
78
96
|
|
@@ -80,9 +98,9 @@
|
|
80
98
|
|
81
99
|
|
82
100
|
|
83
|
-
テキストのサンプルコードも動かしましたが
|
101
|
+
テキストのサンプルコードも動かしましたが同様に動きませんでした。サポートページにも何も書かれていなかったためわかりませんでした。
|
84
102
|
|
85
|
-
pase
|
103
|
+
サンプルコードののfor url in response.scc('a.entry-link::attr("href")')をfor url in response.css('.entrylist-contents-title a::attr("herf")')やfor url in response.css('a.entrylist-contents-title::attr("herf")'),for url in response.css('.entrylist-contents-title a::attr("herf")')に書き換えてみたりしましたが動きませんでした。
|
86
104
|
|
87
105
|
|
88
106
|
|
2
文書の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -4,7 +4,9 @@
|
|
4
4
|
|
5
5
|
python クローリング&スクレイピングーデータ収集・解析のための実践開発ガイドーというテキストのp223のreadabiltyを利用するSpider1の実装に詰まっています。個別ページをたどる関数(pase)と個別webページをパースする関数(pase_page)が動かない状態です。
|
6
6
|
|
7
|
-
|
7
|
+
エントリーページ内のURL取得してfor文を回したいのですがどう書いたらいいのかわかりません。
|
8
|
+
|
9
|
+
ご教授お願い致します。
|
8
10
|
|
9
11
|
|
10
12
|
|
@@ -80,7 +82,7 @@
|
|
80
82
|
|
81
83
|
テキストのサンプルコードも動かしましたがどちらの関数も動いていませんでした。
|
82
84
|
|
83
|
-
pase関数はfor文が動きませんでした。ローリングするwebのにはentry-linkはなくentrylistがあったので書き換えてみましたが動きませんでした。
|
85
|
+
pase関数はfor文が動きませんでした。ローリングするwebのにはentry-linkはなく、entrylistがあったので書き換えてみましたが動きませんでした。
|
84
86
|
|
85
87
|
|
86
88
|
|
1
書式の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -30,13 +30,7 @@
|
|
30
30
|
|
31
31
|
|
32
32
|
|
33
|
-
### 該当のソースコード
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
33
|
+
### python
|
38
|
-
|
39
|
-
# -*- coding: utf-8 -*-
|
40
34
|
|
41
35
|
import scrapy
|
42
36
|
|
@@ -86,7 +80,7 @@
|
|
86
80
|
|
87
81
|
テキストのサンプルコードも動かしましたがどちらの関数も動いていませんでした。
|
88
82
|
|
89
|
-
pase関数はfor文が
|
83
|
+
pase関数はfor文が動きませんでした。ローリングするwebのにはentry-linkはなくentrylistがあったので書き換えてみましたが動きませんでした。
|
90
84
|
|
91
85
|
|
92
86
|
|