首页 > 解决方案 > 如何使用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)

标签: pythonpython-3.xweb-scrapingbeautifulsoupweb-crawler

解决方案


下面是提取作者姓名的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'])

推荐阅读