In [ ]:
Copied!
import requests
from bs4 import BeautifulSoup
import requests
from bs4 import BeautifulSoup
In [ ]:
Copied!
url = 'https://news.cnyes.com/news/id/3949102'
url = 'https://news.cnyes.com/news/id/3949102'
In [ ]:
Copied!
r = requests.get(url)
soup = BeautifulSoup(r.text, 'lxml')
r = requests.get(url)
soup = BeautifulSoup(r.text, 'lxml')
In [ ]:
Copied!
soup.select('div[itemprop="articleBody"]')[0].text
soup.select('div[itemprop="articleBody"]')[0].text
請抓取底下連結的新聞文章,並存成一個文字檔。
url = 'https://tw.finance.appledaily.com/realtime/20180121/1283033/'
【注意】:蘋果新聞的網頁內容已經改變,底下的抓法需要更新!
In [ ]:
Copied!
url = 'https://tw.finance.appledaily.com/realtime/20180121/1283033/'
url = 'https://tw.finance.appledaily.com/realtime/20180121/1283033/'
In [ ]:
Copied!
r = requests.get(url)
soup = BeautifulSoup(r.text, 'lxml')
r = requests.get(url)
soup = BeautifulSoup(r.text, 'lxml')
In [ ]:
Copied!
news = soup.select('div.ndArticle_contentBox > article > div.ndArticle_margin > p')[0].text
news = soup.select('div.ndArticle_contentBox > article > div.ndArticle_margin > p')[0].text
In [ ]:
Copied!
with open("news.txt","w", encoding="utf-8") as f:
f.write(news)
with open("news.txt","w", encoding="utf-8") as f:
f.write(news)