简体   繁体   English

使用 beautifulsoup 获取特定文本

[英]Get a specific text with beautifulsoup

It's an easy enough thing, but I can't do it... I'm trying to discard the data from Reuters.这是一件很容易的事情,但我做不到......我正试图丢弃路透社的数据。 The main problem is that using beautifulsoup, I get all the html tags of the line:主要问题是使用 beautifulsoup,我得到了该行的所有 html 标签:

import requests
from bs4 import BeautifulSoup

URL = 'https://www.reuters.com/markets/stocks/europe'

page = requests.get(URL)
soup = BeautifulSoup(page.content, 'html.parser')
results = soup.find(id='__next')

stock_elems = results.find_all('tr', class_='data')

index_elem = stock_elems[0].find('a', class_='TextLabel__text-label___3oCVw TextLabel__black-to-orange___23uc0 TextLabel__medium___t9PWg MarketsTable-name-1U4vs')
print(index_elem)

I'd like to get something like:我想得到类似的东西:

FTSE 100

But I only got all tag line:但我只得到了所有的标语:

<a class="TextLabel__text-label___3oCVw TextLabel__black-to-orange___23uc0 TextLabel__medium___t9PWg MarketsTable-name-1U4vs" href="/quote/.FTSE">FTSE 100 Index</a>"

I tryied also with other texts in the page and I get the same result:我还尝试了页面中的其他文本,得到了相同的结果:

index_elem = stock_elems[0].find('div', class_='TextLabel__text-label___3oCVw TextLabel__gray___1V4fk TextLabel__regular___2X0ym MarketsTable-subcell-l_NnB')
<div class="TextLabel__text-label___3oCVw TextLabel__gray___1V4fk TextLabel__regular___2X0ym MarketsTable-subcell-l_NnB">.FTSE</div>

Thanks for helping and time感谢您的帮助和时间

You need to add.text to get the text values of the tag您需要 add.text 来获取标签的文本值

import requests
from bs4 import BeautifulSoup

URL = 'https://www.reuters.com/markets/stocks/europe'

page = requests.get(URL)
soup = BeautifulSoup(page.content, 'html.parser')
results = soup.find(id='__next')

stock_elems = results.find_all('tr', class_='data')

index_elem = stock_elems[0].find('a', class_='TextLabel__text-label___3oCVw TextLabel__black-to-orange___23uc0 TextLabel__medium___t9PWg MarketsTable-name-1U4vs')
print(index_elem.text)

Output Output

FTSE 100 Index

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM