當給定一個 url 時,我試圖從 bbc 新聞文章中抓取標題、日期和文章內容。
文章內容在 ssrcss-1q0x1qg-Paragraph css 類的多個 div 中。文章內容進入內容變數。如果沒有 for 回圈,內容只會分配第一個 div 的內容。
import requests
from bs4 import BeautifulSoup
def scraper():
link = 'https://www.bbc.co.uk/news/uk-52255054'
page = requests.get(link)
soup = BeautifulSoup(page.content, 'html.parser')
results = soup.find(class_='ssrcss-pv1rh6-ArticleWrapper')
body = results.find_all(attrs={'class': 'ssrcss-1q0x1qg-Paragraph'})
content = []
for div in body:
paras = div.text
content.append(paras)
return content
如果我只是將抓取的內容列印到控制臺,則格式是完美的,但是當我將其分配并附加到內容變數時,它會以隨機 \ 字符和額外的 ', ' 結尾,因為它是一個串列。請參閱下面的摘錄。
"\"Coronavirus will not overcome us,\" the Queen has said, in an Easter message to the nation.",
"While celebrations would be different for many this year, she said: \"We need Easter as much as ever.\"",
"Referencing the tradition of lighting candles to mark the occasion, she said: \"As dark as death can be - particularly for those suffering with grief - light and life are greater.\"",
"It comes as the number of coronavirus deaths in UK hospitals reached 9,875.",
"Speaking from Windsor Castle, the Queen said many religions had festivals celebrating light overcoming darkness, which often featured the lighting of candles.",
"She said: \"They seem to speak to every culture, and appeal to people of all faiths, and of none.",
"\"They are lit on birthday cakes and to mark family anniversaries, when we gather happily around a source of light. It unites us.\"",
有沒有辦法洗掉這些以使串列成為一個文本塊?
謝謝
uj5u.com熱心網友回復:
\用于轉義引號。- 串列中的
,分隔專案不是專案本身的一部分(在本例中為字串)。
如果要獲取全文,每個字串content用空格分隔,請在代碼末尾添加以下行:
full_text = ' '.join(scraper())
print(full_text)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/496242.html
上一篇:提供None抓取地址
