質問編集履歴

2

実際のコードを記載しました

2018/04/13 06:50

投稿

RWW
RWW

スコア13

test CHANGED
File without changes
test CHANGED
@@ -92,7 +92,9 @@
92
92
 
93
93
  以下エラーがでる箇所
94
94
 
95
+ ```
96
+
95
- ```#カテゴリ毎にURLを解析する
97
+ #カテゴリ毎にURLを解析する
96
98
 
97
99
  base = "https://www.gucci.com/jp/ja/"
98
100
 

1

実際のコードを記載させていただきました。

2018/04/13 06:50

投稿

RWW
RWW

スコア13

test CHANGED
File without changes
test CHANGED
@@ -10,27 +10,103 @@
10
10
 
11
11
 
12
12
 
13
- コードは実際ののではなく、わかりやすように書き換えます。
13
+ プログラミング初心者のため。コードもく、見にくものとなっおります。申し訳ございません。
14
+
15
+
14
16
 
15
17
  ```ここに言語を入力
16
18
 
19
+ import requests
20
+
21
+ from bs4 import BeautifulSoup
22
+
23
+ import pprint
24
+
25
+ import os
26
+
17
- dict = {a,"http://~",b:"http://~"}
27
+ from urllib.parse import urljoin
28
+
29
+ import itertools
18
30
 
19
31
 
20
32
 
21
- base = "https://www.~"
33
+ sex_URL = "https://www.gucci.com/jp/ja/ca/men-c-men"
22
34
 
23
- list = []
35
+ soup = BeautifulSoup(requests.get(sex_URL).content,'lxml')
24
36
 
25
- for name in category:
26
37
 
27
- soup= BeautifulSoup(requests.get(dict[name]).content,'lxml')
28
38
 
29
- link = soup.find_all("a",class_="item-link")
39
+ def make_folder(category_folder_name):
30
40
 
31
- for link in link:
41
+ os.makedirs("/Users/RP/Desktop/GUCCI/メンズ/" + category_folder_name,exist_ok=True)
32
42
 
43
+
44
+
45
+ #カテゴリ毎のURLを取得し、相対パスを絶対パスに変換
46
+
47
+ #item_category_listにURLを格納
48
+
49
+ base = "https://www.gucci.com/jp/ja/"
50
+
51
+ item_category_link = soup.find_all("a",class_="category-product")
52
+
53
+ item_category_list = []
54
+
55
+ for item_category_link in item_category_link:
56
+
57
+ item_category_list.append(urljoin(base,item_category_link.get("href")))
58
+
59
+ print(item_category_list)
60
+
61
+
62
+
63
+ #商品カテゴリ名を取得し、item_category_name_listに格納
64
+
65
+ item_category_name = soup.select("header > h2")
66
+
67
+ item_category_name_list = []
68
+
69
+ for item_category_name in item_category_name:
70
+
71
+ item_category_name_list.append((item_category_name).getText())
72
+
73
+ make_folder((item_category_name).getText())
74
+
75
+ print(item_category_name_list)
76
+
77
+
78
+
79
+ #item_category_listとitem_category_name_listのリストを1つのディクショナリに変換
80
+
81
+ keys = item_category_name_list
82
+
83
+ values = item_category_list
84
+
85
+ category = dict(zip(keys,values))
86
+
87
+ print(category)
88
+
89
+ ```
90
+
91
+
92
+
93
+ 以下エラーがでる箇所
94
+
95
+ ```#カテゴリ毎にURLを解析する
96
+
97
+ base = "https://www.gucci.com/jp/ja/"
98
+
99
+ item_list = []
100
+
101
+ for category_name in category:
102
+
103
+ soup= BeautifulSoup(requests.get(category[category_name]).content,'lxml')
104
+
105
+ item_link = soup.find_all("a",class_="product-tiles-grid-item-link")
106
+
107
+ for item_link in item_link:
108
+
33
- item_list.append(urljoin(base,link).get("href"))
109
+ item_list.append(urljoin(base,item_link).get("href"))
34
110
 
35
111
  print(item_list)
36
112