teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

テキスト修正

2018/10/07 14:22

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -37,13 +37,13 @@
37
37
 
38
38
  with open(path) as f:
39
39
  soup = BeautifulSoup(f.read(), 'html.parser')
40
+ links = soup.find(class_='post_loop').find_all('a')
41
+ for link in links:
42
+ if 'href' in link.attrs:
43
+ title = link.find(class_='post_title').find(text=True).strip()
44
+ view = link.find(class_='view').find(text=True).strip().split()[0]
45
+ print(','.join([title, link['href'], view]))
40
46
 
41
- links = soup.find(class_='post_loop').find_all('a')
42
- for link in links:
43
- if 'href' in link.attrs:
44
- title = link.find(class_='post_title').find(text=True).strip()
45
- view = link.find(class_='view').find(text=True).strip().split()[0]
46
- print(','.join([title, link['href'], view]))
47
47
  ```
48
48
 
49
49
  > (teratail) [jun68ykt@macmini q150619]$ **python scraping.py > data.csv**

1

テキスト修正

2018/10/07 14:22

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  - Pythonのソースコード内では `print()` を使い、実行時にファイルにリダイレクトすることでCSVファイルを作成
6
6
 
7
- - スクリプトの作成中、頻繁に対象サイトにアクセスしないよう、解析対象のページのソースをダウンロードして、これを使用( index.html というHTMLファイルに保存
7
+ - スクリプトの作成中、頻繁に対象サイトにアクセスしないよう、解析対象のページをダウンロードして、index.html というHTMLファイルに保存してこれを解析対象とする。
8
8
 
9
9
  以下はスクリプトの内容と実行ログです。
10
10