如何使 URL 列中的链接可点击
How can I make the links in the URL column clickable
重要的是不要显示整个URL,而可能只显示“To the article”,以便存放“To the article”后面的URL?
googlenews = GoogleNews()
googlenews.set_encode('utf_8')
for ort in orte:
googlenews.clear()
googlenews.get_news(ort)
table_new = []
for row in googlenews.results():
table_new.append({
'City': ort,
'Title': row['title'],
'Date': row['date']})
df = pd.DataFrame(table_new)
nachrichten.append(df)
dfges = pd.concat(nachrichten, axis='index')
print(dfges)
´´´
您的原始网址无效。要将它们变成有效的 Google 新闻 URL,您需要添加 row['link'].replace('/./article', '/article')
并添加 https://
前缀。获得真实 link 的选项已被讨论 here.。
这将变成:
news.google.com/./articles/CBMigQFodHRwczovL3d3dy5lc3BuLmNvbS9zb2NjZXIvZ2VybWFuLWJ1bmRlc2xpZ2Evc3RvcnkvNDYzNDMxOC91bmNvbWZvcnRhYmxlLWZyZWlidXJnLWFwcGVhbC1hZnRlci1iYXllcm4tbXVuaWNoLXN1YnN0aXR1dGlvbi1taXgtdXDSAY4BaHR0cHM6Ly93d3cuZXNwbi5jb20vc29jY2VyL2dlcm1hbi1idW5kZXNsaWdhL3N0b3J5LzQ2MzQzMTgvdW5jb21mb3J0YWJsZS1mcmVpYnVyZy1hcHBlYWwtYWZ0ZXItYmF5ZXJuLW11bmljaC1zdWJzdGl0dXRpb24tbWl4LXVwP3BsYXRmb3JtPWFtcA?hl=en-US&gl=US&ceid=US%3Aen
进入:
https://news.google.com/articles/CBMigQFodHRwczovL3d3dy5lc3BuLmNvbS9zb2NjZXIvZ2VybWFuLWJ1bmRlc2xpZ2Evc3RvcnkvNDYzNDMxOC91bmNvbWZvcnRhYmxlLWZyZWlidXJnLWFwcGVhbC1hZnRlci1iYXllcm4tbXVuaWNoLXN1YnN0aXR1dGlvbi1taXgtdXDSAY4BaHR0cHM6Ly93d3cuZXNwbi5jb20vc29jY2VyL2dlcm1hbi1idW5kZXNsaWdhL3N0b3J5LzQ2MzQzMTgvdW5jb21mb3J0YWJsZS1mcmVpYnVyZy1hcHBlYWwtYWZ0ZXItYmF5ZXJuLW11bmljaC1zdWJzdGl0dXRpb24tbWl4LXVwP3BsYXRmb3JtPWFtcA?hl=en-US&gl=US&ceid=US%3Aen
要使 URL 可点击,您可以按照建议添加以下代码 :
def make_clickable(val):
return '<a href="{}">{}</a>'.format(val,'To the article')
dfges.style.format({'URL': make_clickable})
完整代码:
import pandas as pd
from GoogleNews import GoogleNews
googlenews = GoogleNews()
googlenews.set_encode('utf_8')
googlenews.set_lang('en')
googlenews.set_period('7d')
orte = ["Munich"]
nachrichten = []
for ort in orte:
googlenews.clear()
googlenews.get_news(ort)
table_new = []
for row in googlenews.results():
table_new.append({
'City': ort,
'Title': row['title'],
'Date': row['date'],
'URL': f"https://{row['link'].replace('/./article', '/article')}",
'Source': row['site'], })
df = pd.DataFrame(table_new)
nachrichten.append(df)
dfges = pd.concat(nachrichten, axis='index')
dfges.drop_duplicates(subset=['Title'], keep='last', inplace=True)
print(dfges)
def make_clickable(val):
return '<a href="{}">{}</a>'.format(val,'To the article')
dfges.style.format({'URL': make_clickable})
输出:
在新选项卡中打开 link,这会将页面重定向到原始文章。
重要的是不要显示整个URL,而可能只显示“To the article”,以便存放“To the article”后面的URL?
googlenews = GoogleNews()
googlenews.set_encode('utf_8')
for ort in orte:
googlenews.clear()
googlenews.get_news(ort)
table_new = []
for row in googlenews.results():
table_new.append({
'City': ort,
'Title': row['title'],
'Date': row['date']})
df = pd.DataFrame(table_new)
nachrichten.append(df)
dfges = pd.concat(nachrichten, axis='index')
print(dfges)
´´´
您的原始网址无效。要将它们变成有效的 Google 新闻 URL,您需要添加 row['link'].replace('/./article', '/article')
并添加 https://
前缀。获得真实 link 的选项已被讨论 here.。
这将变成:
news.google.com/./articles/CBMigQFodHRwczovL3d3dy5lc3BuLmNvbS9zb2NjZXIvZ2VybWFuLWJ1bmRlc2xpZ2Evc3RvcnkvNDYzNDMxOC91bmNvbWZvcnRhYmxlLWZyZWlidXJnLWFwcGVhbC1hZnRlci1iYXllcm4tbXVuaWNoLXN1YnN0aXR1dGlvbi1taXgtdXDSAY4BaHR0cHM6Ly93d3cuZXNwbi5jb20vc29jY2VyL2dlcm1hbi1idW5kZXNsaWdhL3N0b3J5LzQ2MzQzMTgvdW5jb21mb3J0YWJsZS1mcmVpYnVyZy1hcHBlYWwtYWZ0ZXItYmF5ZXJuLW11bmljaC1zdWJzdGl0dXRpb24tbWl4LXVwP3BsYXRmb3JtPWFtcA?hl=en-US&gl=US&ceid=US%3Aen
进入:
https://news.google.com/articles/CBMigQFodHRwczovL3d3dy5lc3BuLmNvbS9zb2NjZXIvZ2VybWFuLWJ1bmRlc2xpZ2Evc3RvcnkvNDYzNDMxOC91bmNvbWZvcnRhYmxlLWZyZWlidXJnLWFwcGVhbC1hZnRlci1iYXllcm4tbXVuaWNoLXN1YnN0aXR1dGlvbi1taXgtdXDSAY4BaHR0cHM6Ly93d3cuZXNwbi5jb20vc29jY2VyL2dlcm1hbi1idW5kZXNsaWdhL3N0b3J5LzQ2MzQzMTgvdW5jb21mb3J0YWJsZS1mcmVpYnVyZy1hcHBlYWwtYWZ0ZXItYmF5ZXJuLW11bmljaC1zdWJzdGl0dXRpb24tbWl4LXVwP3BsYXRmb3JtPWFtcA?hl=en-US&gl=US&ceid=US%3Aen
要使 URL 可点击,您可以按照建议添加以下代码
def make_clickable(val):
return '<a href="{}">{}</a>'.format(val,'To the article')
dfges.style.format({'URL': make_clickable})
完整代码:
import pandas as pd
from GoogleNews import GoogleNews
googlenews = GoogleNews()
googlenews.set_encode('utf_8')
googlenews.set_lang('en')
googlenews.set_period('7d')
orte = ["Munich"]
nachrichten = []
for ort in orte:
googlenews.clear()
googlenews.get_news(ort)
table_new = []
for row in googlenews.results():
table_new.append({
'City': ort,
'Title': row['title'],
'Date': row['date'],
'URL': f"https://{row['link'].replace('/./article', '/article')}",
'Source': row['site'], })
df = pd.DataFrame(table_new)
nachrichten.append(df)
dfges = pd.concat(nachrichten, axis='index')
dfges.drop_duplicates(subset=['Title'], keep='last', inplace=True)
print(dfges)
def make_clickable(val):
return '<a href="{}">{}</a>'.format(val,'To the article')
dfges.style.format({'URL': make_clickable})
输出:
在新选项卡中打开 link,这会将页面重定向到原始文章。