回答編集履歴

3

少しお行儀よく

2018/06/11 07:13

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -50,20 +50,22 @@
50
50
 
51
51
  forブロックのアタマで`x = x['href']`としておけば良いかと。
52
52
 
53
-
54
-
55
- あるいは、次のように書くのもアリです。
56
-
57
53
  ```Python
58
54
 
59
- from operator import itemgetter
55
+ from time import sleep
60
56
 
61
57
 
62
58
 
59
+ for x in url_items:
60
+
63
- for x in map(itemgetter('href'), url_items):
61
+ x = x['href']
64
62
 
65
63
  print(x, type(x))
66
64
 
67
65
  ...
68
66
 
67
+
68
+
69
+ sleep(1) # 相手方に負荷をかけないため
70
+
69
71
  ```

2

逃げ道作っとくやつ

2018/06/11 07:12

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -19,6 +19,10 @@
19
19
  追記
20
20
 
21
21
  ---
22
+
23
+ BeautifulSoupはお試し程度にしか使ったことがないのでベストな方法ではないかもです。悪しからず。
24
+
25
+
22
26
 
23
27
  > ```
24
28
 

1

追記

2018/06/11 07:00

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -13,3 +13,53 @@
13
13
  html = req.urlopen(x)
14
14
 
15
15
  ```
16
+
17
+
18
+
19
+ 追記
20
+
21
+ ---
22
+
23
+ > ```
24
+
25
+ Traceback (most recent call last):
26
+
27
+ File "base.py", line 11, in <module>
28
+
29
+ html = req.urlopen(x)
30
+
31
+ File "C:\Users(※ここはユーザーネームです隠しています)\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py", line 223, in urlopen
32
+
33
+ return opener.open(url, data, timeout)
34
+
35
+ File "C:\Users\※ここはユーザーネームです隠しています\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py", line 521, in open
36
+
37
+ meth_name = protocol+"_request"
38
+
39
+ TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
40
+
41
+ > ```
42
+
43
+
44
+
45
+ print(x, type(x))すると分かりますが、xはelement.Tag型です。
46
+
47
+ forブロックのアタマで`x = x['href']`としておけば良いかと。
48
+
49
+
50
+
51
+ あるいは、次のように書くのもアリです。
52
+
53
+ ```Python
54
+
55
+ from operator import itemgetter
56
+
57
+
58
+
59
+ for x in map(itemgetter('href'), url_items):
60
+
61
+ print(x, type(x))
62
+
63
+ ...
64
+
65
+ ```