Guest User

Untitled

a guest
Mar 19th, 2024
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.97 KB | None | 0 0
  1. import csv
  2. from newsplease import NewsPlease
  3.  
  4. # Define the list of article URLs
  5. article_urls = [
  6.     "https://www.chinadaily.com.cn/a/202403/18/WS65f7953ba31082fc043bd1ff.html",
  7.     "https://www.chinadaily.com.cn/a/202403/11/WS65ee6fb3a31082fc043bbdfb.html",
  8.     "https://global.chinadaily.com.cn/a/202312/04/WS656d3979a31090682a5f1495.html",
  9.     "https://global.chinadaily.com.cn/a/202305/22/WS646b08a1a310b6054fad467d.html",
  10.     "https://www.globaltimes.cn/page/202403/1308306.shtml",
  11.     "https://www.globaltimes.cn/page/202403/1308489.shtml",
  12.     "https://www.globaltimes.cn/page/202304/1289190.shtml",
  13.     "https://news.cgtn.com/news/2023-09-28/Analysis-Will-the-world-accept-the-Huawei-subculture--1ntsjwCb5Sg/index.html",
  14.     "https://news.cgtn.com/news/2020-09-16/Can-Huawei-survive-the-U-S-chip-ban--TPVMNJdWq4/index.html",
  15.     "https://news.cgtn.com/news/2019-06-29/Ending-the-Huawei-ban-is-common-sense-HVoQ4nz3Xi/index.html",
  16. ]
  17.  
  18. # Create a list to store the article information
  19. articles_data = []
  20.  
  21. # Iterate over the article URLs
  22. for url in article_urls:
  23.     # Use news-please to extract information from the article
  24.     article = NewsPlease.from_url(url)
  25.  
  26.     # Extract the desired information
  27.     title = article.title
  28.     authors = ', '.join(article.authors) if article.authors else "Unknown"
  29.     date = article.date_publish if article.date_publish else "Unknown"
  30.     source = url.split("//")[-1].split("/")[0] # Extract the domain as the source
  31.  
  32.     # Append the article information to the list
  33.     articles_data.append([title, authors, date, source, url])
  34.  
  35. # Define the CSV file path
  36. csv_file = "articles_tech.csv"
  37.  
  38. # Write the article information to the CSV file
  39. with open(csv_file, mode='w', newline='', encoding='utf-8') as file:
  40.     writer = csv.writer(file)
  41.     writer.writerow(["Title", "Authors", "Publication Date", "Source", "URL"])
  42.  
  43.     for article_data in articles_data:
  44.         writer.writerow(article_data)
  45.  
  46. print("Data exported to", csv_file)
  47.  
  48.  
Advertisement
Add Comment
Please, Sign In to add comment