2017-04-19 7 views
1

は、私は、次の試してみた:Python 3で記事サイトのテキストコンテンツを抽出するにはどうすればよいですか?

import urllib 

link = 'https://automatetheboringstuff.com/chapter7/' 
f = urllib.request.urlopen(link) 
myfile = f.read() 
print(myfile) 

しかし、それは単なるテキストコンテンツではなく、ページのソースを返すようです。

+1

あなたはその –

+0

ためBeautifulSoup'は ''正しいurllib.request.urlopen(リンク)です '必要がありますか? – bhansa

答えて

1

チャプターテキストのみを取得したい場合は、美しいスープを選んだと思います。あなたのケースでは

import requests 
from bs4 import BeautifulSoup 

res = requests.get('https://automatetheboringstuff.com/chapter7/') 
soup = BeautifulSoup(res.text, 'html.parser') 
print(soup.find('div', { "class" : "book" }).text) 
関連する問題