python - Python Newspaper 函数在循环期间未读取文章 URL?
问题描述
如果这是一个愚蠢的问题,我深表歉意——我是 Python 新手,并且更熟悉 excel VBA。
我试图让 Python 循环遍历 Excel 文档中的多个文章 URL,并创建各种 URL 的摘要。目标是将文章标题、摘要和 URL 导出到新的 Excel(或不同的选项卡)。(最终目标是搜集相关新闻并进行总结,但我正在努力实现这一目标!)
但是,我在让 Newspaper Article 函数读取从我创建的列表中传递的 URL 时遇到问题。当我打印 URL 时,它看起来就像我刚刚复制粘贴并设置 url = '复制粘贴的值'。但是,当我在该 URL 上运行“文章”功能时,它似乎没有正确读取 URL。它们作为字符串存储在列表中。不知道我可能做错了什么。任何帮助,将不胜感激!!
# Import the libraries
import nltk
from newspaper import Article
import openpyxl
# import the URLs from the Excel
from openpyxl import load_workbook
wb = load_workbook(r'C:\Users\Python\RunPythonScript.xlsm') # Work Book
ws = wb.get_sheet_by_name('URLs') # Work Sheet
column = ws['A'] # Column
column_list = [column[x].value for x in range(len(column))] # create a list
url_list = list(filter(None, column_list)) # remove blanks
url_list.pop(0) # remove title
# start loop
x = 0
while x < len(url_list):
url = str("'" + url_list[x] + "'") # set url
article = Article(url) # Get the article ### seems to be where error is ###
print(article)
x = x + 1 # move to next url
我从 python 得到以下输出:
<newspaper.article.Article object at 0x07DADB38>
<newspaper.article.Article object at 0x0A698670>
<newspaper.article.Article object at 0x07DADB38>
<newspaper.article.Article object at 0x0A698670>
<newspaper.article.Article object at 0x07DADB38>
<newspaper.article.Article object at 0x0A698670>
<newspaper.article.Article object at 0x07DADB38>
<newspaper.article.Article object at 0x0A698670>
<newspaper.article.Article object at 0x07DADB38>
<newspaper.article.Article object at 0x0A698670>
它似乎没有打印文章,而是在 URL 上出错。
有什么见解吗?提前致谢!!
解决方案
文档https://newspaper.readthedocs.io/en/latest/非常清楚。
看来您必须将代码修改为以下内容:
...
while x < len(url_list):
url = str("'" + url_list[x] + "'") # set url
article = Article(url)
article.download()
article.parse()
print(article.authors)
print(article.publish_date)
print(article.text)
print(article.top_image)
# And so on and so far...
x = x + 1 # move to next url
推荐阅读
- flutter - 如何在flutter中对sqflite数据库进行读写
- sql - 不满足条件时添加虚拟数据
- arduino - 将 Esp8266 客户端连接到本地 Web 服务器的子目录
- spring - 此处不允许注释 - Intellij
- excel - Power Query - 根据 excel 单元格添加列数量
- python - pytorch 加载 _IncompatibleKeys
- c# - FluentValidation 单个命令的多个验证器
- python - 自定义 X 轴 Matplotlib Python
- electron - 在基于电子的桌面应用程序中查找用户桌面空闲时间的最佳方法
- python - 匹配条件错误,字母“b”应该至少有一个前导使用正则表达式 python。如何纠正它?