【问题标题】:How to extract the highlighted info from the html tags using python?如何使用python从html标签中提取突出显示的信息?
【发布时间】:2020-12-31 19:48:51
【问题描述】:

我只想抓取一个网站并提取图像中突出显示的标签内的“作者姓名”。如何使用python3做到这一点?我遇到了困难,因为作者的名字在多个标签中。 (image with the highlighted part)

这是我为提取“标题”和“日期”而编写的代码。现在,我要提取作者姓名。

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 python-3.x web-scraping beautifulsoup web-crawler


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多