python - 如何使用python从html标签中提取突出显示的信息?
问题描述
我只想抓取一个网站并提取标签内的“作者姓名”,如图像中突出显示的那样。如何使用python3做到这一点?我遇到了困难,因为作者的名字在多个标签中。(带有突出显示部分的图像)
这是我为提取“标题”和“日期”而编写的代码。现在,我想提取作者姓名。
from urllib.request import urlopen
from htmldate import find_date
url = "https://indianexpress.com/article/business/companies/market-surges-
after-report-says-amazon-looking-at-40-in-reliance-retail-6591325/"
page = urlopen(url)
#print(page)
html_bytes = page.read()
html = html_bytes.decode("utf-8")
#print(html)
title_index = html.find("<title>")
start_index = title_index + len("<title>")
end_index = html.find("</title>")
title = html[start_index:end_index]
print(title)
date = find_date(url)
print(date)
解决方案
下面是提取作者姓名的python代码。
import json
import requests
from bs4 import BeautifulSoup
fb_url = requests.get('https://www.facebook.com/indianexpress').text
soup = BeautifulSoup(fb_url,'lxml')
data = soup.find('script', type='application/ld+json')
data = json.loads(data.text)
print(data['name'])
推荐阅读
- excel - 查明之前是否在 2 列中输入了相邻的 2 个单元格值的组合
- mongodb - Docker 运行不工作
- java - 给定矩阵中最大岛屿的算法
- swift - iOS-Charts 库:没有显示支持数据的 x 轴标签
- build - 将 pandoc 构建到 yocto core-image-minimal
- c# - 对于 web api 2.2 解决方案,您将如何处理这个特定的 api 补丁版本控制?
- c# - 无法将 System.Double 强制转换为 System.Object。LINQ to Entities 仅支持转换 EDM 基元或枚举类型
- javascript - 更改标签 Chart.js 的背景颜色
- python - 类属性转换
- c++ - OpenGL 项目给出错误“进程以退出代码 -1073741515 (0xC0000135) 完成”