Python 当我尝试使用BeautifulSoup从网站上抓取时,文本丢失

Python 当我尝试使用BeautifulSoup从网站上抓取时,文本丢失,python,beautifulsoup,Python,Beautifulsoup,我试图从伦敦证券交易所的一篇新闻文章中摘取主要文本,但当我试图使用BeautifulSoup将其拉出来时,它却没有出现。有人知道我怎样才能得到这些信息吗 我可以在单击inspect时找到标记,但是当我查看源代码(Ctrl+U)时,文本不会出现。我认为这些信息可能是从另一个网站加载到这个网站上的,但是我不确定这一点,也不知道如何抓取它 我正在浏览的网站是: 我正在尝试提取有关临时结果的主要内容。文章存储在标签内的页面中。您可以使用此示例来提取它: import json import reques

我试图从伦敦证券交易所的一篇新闻文章中摘取主要文本,但当我试图使用BeautifulSoup将其拉出来时,它却没有出现。有人知道我怎样才能得到这些信息吗

我可以在单击inspect时找到标记,但是当我查看源代码(Ctrl+U)时,文本不会出现。我认为这些信息可能是从另一个网站加载到这个网站上的,但是我不确定这一点,也不知道如何抓取它

我正在浏览的网站是:


我正在尝试提取有关临时结果的主要内容。

文章存储在
标签内的页面中。您可以使用此示例来提取它:

import json
import requests
from bs4 import BeautifulSoup


url = 'https://www.londonstockexchange.com/news-article/PFG/interim-results-for-six-months-ended-30-june-2020/14665452'

soup = BeautifulSoup(requests.get(url).content, 'html.parser')
data = soup.select_one('#ng-lseg-state').string.replace('&q;', '"').replace('&l;', '<').replace('&g;', '>').replace('&a;', '&').replace('&s;', "'")
data = json.loads(data)

# uncomment this to print all data:
# print(json.dumps(data, indent=4))

def find_news_article(data):
    if isinstance(data, dict):
        for k, v in data.items():
            if k == 'newsArticle':
                yield v
            else:
                yield from find_news_article(v)
    elif isinstance(data, list):
        for v in data:
            yield from find_news_article(v)

article = BeautifulSoup(next(find_news_article(data))['value'], 'html.parser')

# print text from article on screen:
print(article.get_text(strip=True, separator='\n'))

文章存储在
标签内的页面中。您可以使用此示例来提取它:

import json
import requests
from bs4 import BeautifulSoup


url = 'https://www.londonstockexchange.com/news-article/PFG/interim-results-for-six-months-ended-30-june-2020/14665452'

soup = BeautifulSoup(requests.get(url).content, 'html.parser')
data = soup.select_one('#ng-lseg-state').string.replace('&q;', '"').replace('&l;', '<').replace('&g;', '>').replace('&a;', '&').replace('&s;', "'")
data = json.loads(data)

# uncomment this to print all data:
# print(json.dumps(data, indent=4))

def find_news_article(data):
    if isinstance(data, dict):
        for k, v in data.items():
            if k == 'newsArticle':
                yield v
            else:
                yield from find_news_article(v)
    elif isinstance(data, list):
        for v in data:
            yield from find_news_article(v)

article = BeautifulSoup(next(find_news_article(data))['value'], 'html.parser')

# print text from article on screen:
print(article.get_text(strip=True, separator='\n'))

请添加一段您正在使用的相关代码和错误的输出/值。请添加一段您正在使用的相关代码和错误的输出/值