我正在尝试编写一个Python脚本来检索一篇文章是否有作者。你知道吗
我写了以下内容:
s = "https://www.nytimes.com/2017/08/18/us/politics/steve-bannon-trump-white-house.html?hp&action=click&pgtype=Homepage&clickSource=story-heading&module=a-lede-package-region®ion=top-news&WT.nav=top-news"
def checkForAuthor():
r = requests.get(s)
return "By" in r.text
print(checkForAuthor())
问题是函数checkForAuthor
返回true
,即使没有作者,因为它搜索整个HTML内容来查找单词。
有没有更好的逻辑来寻找作者而不必搜索整个文档?例如在标题中搜索,这样我甚至不必搜索文章内容。我确实需要使这个一般,以便任何网站,我去搜索它会给我的结果。不知道外面有没有这样的东西。你知道吗
要解析html并查找所需的数据,应该使用
BeautifulSoup
库。你知道吗在URL的html中,有一个带有作者的
meta
标记:因此,要检查是否有作者,您需要通过其名称(
byl
)找到它:实际上,您还可以通过
meta["content"]
获得作者名称从网页中抓取数据的一个关键部分是查看网页的HTML源以正确地获取数据。在您提供的链接中,有以下几行包含作者信息。你知道吗
还有其他的,但这些应该会有所帮助。要解析这些标记,可以使用beautiful-soup。你知道吗
相关问题 更多 >
编程相关推荐