質問編集履歴

2

改行を消すコード追記

2018/10/19 02:30

投稿

Yukiya025
Yukiya025

スコア86

test CHANGED
File without changes
test CHANGED
@@ -177,3 +177,39 @@
177
177
  400
178
178
 
179
179
  ```
180
+
181
+
182
+
183
+ # 改行を消すコード
184
+
185
+ 改行を消すコードを[barobaro](https://teratail.com/users/barobaro)様に教えてもらいました! ありがとうございます<3
186
+
187
+
188
+
189
+ **改行を消すコード**
190
+
191
+ ```python
192
+
193
+ def getpret_teratail():
194
+
195
+ r1 = requests.get("https://www.oreilly.co.jp/books/9784873118574/")
196
+
197
+ r1.encoding = r1.apparent_encoding
198
+
199
+
200
+
201
+ html_doc = r1.text
202
+
203
+ soup = BeautifulSoup(html_doc)
204
+
205
+ print(soup.find(itemprop="author").text)
206
+
207
+ print(soup.find(itemprop="datePublished").parent.get_text(strip=True))
208
+
209
+ print(soup.find(itemprop = "numberOfPages").text)
210
+
211
+
212
+
213
+ getpret_teratail()
214
+
215
+ ```

1

完成コード追記

2018/10/19 02:30

投稿

Yukiya025
Yukiya025

スコア86

test CHANGED
File without changes
test CHANGED
@@ -105,3 +105,75 @@
105
105
 
106
106
 
107
107
  ```
108
+
109
+
110
+
111
+ # できましたー(≧∇≦)b
112
+
113
+ [hayataka2049](https://teratail.com/users/hayataka2049)様のアドバイスのおかげでできましたー(>ω<)
114
+
115
+
116
+
117
+ ```python
118
+
119
+ # 完成コード
120
+
121
+ def getpret():
122
+
123
+ r1 = requests.get("https://www.oreilly.co.jp/books/9784873118574/")
124
+
125
+ r1.encoding = r1.apparent_encoding
126
+
127
+
128
+
129
+ html_doc = r1.text
130
+
131
+ soup = BeautifulSoup(html_doc)
132
+
133
+ print(soup.find(itemprop="author").text)
134
+
135
+ print(soup.find(itemprop="datePublished")["content"])
136
+
137
+ print(soup.find(itemprop = "numberOfPages").text)
138
+
139
+
140
+
141
+ getpret()
142
+
143
+ ```
144
+
145
+
146
+
147
+
148
+
149
+
150
+
151
+ **もうひとつの候補**
152
+
153
+ ```
154
+
155
+ print(soup.find(itemprop="author").text)
156
+
157
+ print(soup.find(itemprop="datePublished").parent.text)
158
+
159
+ print(soup.find(itemprop = "numberOfPages").text)
160
+
161
+ ```
162
+
163
+ ちなみに上記のコードにすると出力結果が以下のようになる(._.)
164
+
165
+ 何故か出力結果に改行。。。
166
+
167
+ ```
168
+
169
+ François Chollet、J. J. Allaire 著、瀬戸山 雅人 監訳、長尾 高弘 訳
170
+
171
+
172
+
173
+ 2018年10月 発行
174
+
175
+
176
+
177
+ 400
178
+
179
+ ```