我想將 bs4.element.ResultSet 中的標題和鏈接提取到 pandas 資料框中:
代碼:
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36'
config = Config()
config.browser_user_agent = user_agent
user_input = "Solarpanels"
site = f'https://news.google.com/rss/search?q={user_input} when:14d&hl=en-GB&gl=DE&ceid=GB:en'
op = urlopen(site)
rd = op.read()
sp_page = soup(rd, 'xml')
news_list = sp_page.find_all('item')
print(type(news_list))
print(news_list)
輸出:
<class 'bs4.element.ResultSet'>
[<item><title>Australian research finds cost-effective way to recycle solar panels - The Guardian</title><link>https://www.theguardian.com/environment/2022/oct/16/australian-research-finds-cost-effective-way-to-recycle-solar-panels</link><guid isPermaLink="false">1605236140</guid><pubDate>Sat, 15 Oct 2022 23:51:00 GMT</pubDate><description><ol><li><a href="https://www.theguardian.com/environment/2022/oct/16/australian-research-finds-cost-effective-way-to-recycle-solar-panels" target="_blank">Australian research finds cost-effective way to recycle solar panels</a>&nbsp;&nbsp;<font color="#6f6f6f">The Guardian</font></li><li><a href="https://www.techjuice.pk/australian-researchers-find-cost-effective-way-to-recycle-solar-panels/" target="_blank">Australian Researchers Find Cost-Effective Way To Recycle Solar Panels</a>&nbsp;&nbsp;<font color="#6f6f6f">TechJuice</font></li><li><a href="https://www.esi-africa.com/industry-sectors/business-and-markets/how-could-recycling-solar-panels-be-scaled-up-for-sustainable-effect/" target="_blank">How could recycling solar panels be scaled up for sustainable effect</a>&nbsp;&nbsp;<font color="#6f6f6f">ESI Africa</font></li><li><a href="https://www.digitaljournal.com/pr/solar-panel-recycling-market-to-rise-at-37-cagr-during-forecast-period-tmr-study" target="_blank">Solar Panel Recycling Market to Rise at 37% CAGR during Forecast Period: TMR Study</a>&nbsp;&nbsp;<font color="#6f6f6f">Digital Journal</font></li><li><strong><a href="https://news.google.com/stories/CAAqNggKIjBDQklTSGpvSmMzUnZjbmt0TXpZd1NoRUtEd2lzNjdmOUJSR3NNT0h4Y0h5dF9TZ0FQAQ?oc=5" target="_blank">View Full coverage on Google News</a></strong></li></ol></description><source url="https://www.theguardian.com">The Guardian</source></item>
... and much more
我嘗試了很多,但不幸的是我做不到。
uj5u.com熱心網友回復:
嘗試:
import requests
import pandas as pd
from bs4 import BeautifulSoup
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36"
}
user_input = "Solarpanels"
site = f"https://news.google.com/rss/search?q={user_input} when:14d&hl=en-GB&gl=DE&ceid=GB:en"
soup = BeautifulSoup(requests.get(site, headers=headers).content, "xml")
all_data = []
for item in soup.select("item"):
all_data.append(
{
"title": item.title.text,
"link": item.link.text,
"pubDate": item.pubDate.text,
"description": BeautifulSoup(
item.description.text, "html.parser"
).get_text(strip=True), # or .get_text(strip=True, separator=" ")
"source": item.source.text,
"source_url": item.source["url"],
}
)
df = pd.DataFrame(all_data)
print(df.head().to_markdown(index=False))
印刷:
| 標題 | 關聯 | 發布日期 | 描述 | 資源 | source_url |
|---|---|---|---|---|---|
| 澳大利亞研究發現回收太陽能電池板的成本效益高的方法 | https://www.theguardian.com/environment/2022/oct/16/australian-research-finds-cost-effective-way-to-recycle-solar-panels | 格林威治標準時間 2022 年 10 月 15 日星期六 23:51:00 | 澳大利亞研究發現回收太陽能電池板的經濟有效方式衛報澳大利亞研究人員發現回收太陽能電池板的經濟有效方式TechJuice如何擴大回收太陽能電池板以實作可持續影響ESI非洲太陽能電池板回收市場在預測期內以 37% 的復合年增長率增長:TMR 研究數字期刊查看全文谷歌新聞報道 | 守護者 | https://www.theguardian.com |
| 商業問題:商業地產上的太陽能電池板:為什么要進行轉換 - 內幕媒體 | https://www.insidermedia.com/blogs/north-west/business-matters-solar-panels-on-commercial-property-why-you-should-make-the-switch | 格林威治標準時間 2022 年 10 月 17 日星期一 09:13:35 | 商業問題:商業地產上的太陽能電池板:為什么你應該制作 SwitchInsider 媒體 | 內幕媒體 | https://www.insidermedia.com |
| 生活成本:人們使用太陽能電池板和渦輪機來減少賬單 - bbc.co.uk | https://www.bbc.co.uk/news/uk-england-essex-62967716 | 格林威治標準時間 2022 年 10 月 5 日星期三 07:00:00 | 生活成本:人們使用太陽能電池板和渦輪機來減少賬單 bbc.co.uk | 英國廣播公司 | https://www.bbc.co.uk |
| 學校申請 120 塊太陽能電池板 - 斯坦福水星 | https://www.stamfordmercury.co.uk/news/school-applies-for-120-solar-panels-9278921/ | 格林威治標準時間 2022 年 10 月 17 日星期一 11:00:00 | 學校申請120塊太陽能電池板斯坦福水星 | 斯坦福水星 | https://www.stamfordmercury.co.uk |
| 太陽能電池板使拉納克郡村大廳的運行成本降低了 80%-每日記錄 | https://www.dailyrecord.co.uk/in-your-area/lanarkshire/solar-panels-enable-lanarkshire-village-28211459 | 格林威治標準時間 2022 年 10 月 16 日星期日 18:50:00 | 太陽能電池板使拉納克郡村大廳的運行成本降低了 80% 每日記錄 | 日志 | https://www.dailyrecord.co.uk |
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/518201.html
下一篇:最快的迭代技術?
