Python 如何从“删除”中删除一些文本;获取“文本();输出单位:U组

Python 如何从“删除”中删除一些文本;获取“文本();输出单位:U组,python,beautifulsoup,python-requests,Python,Beautifulsoup,Python Requests,我正在制作一个网络抓取程序,从IG Markets获取零售交易情绪 我希望在控制台中显示的输出是: "EUR/USD: 57% of clients accounts are short on this market". 我现在得到的结果是: "EUR/USD: 57% of client accounts are short on this market The percentage of IG client accou

我正在制作一个网络抓取程序,从IG Markets获取零售交易情绪

我希望在控制台中显示的输出是:

     "EUR/USD: 57% of clients accounts are short on this market".
我现在得到的结果是:

     "EUR/USD:  57% of client accounts are short on this market The percentage of IG client    
      accounts with positions in this market that are currently long or short. Calculated  
      to the nearest 1%."
如何删除此文本:

        "The percentage of IG client accounts with positions in this market that are  
         currently long or short. Calculated to the nearest 1%."
多谢各位

代码如下:

import bs4, requests

def getIGsentiment(pairUrl):
    res = requests.get(pairUrl)
    res.raise_for_status()

    soup = bs4.BeautifulSoup(res.text, 'html.parser')
    elems = soup.select('.price-ticket__sentiment')
    return elems[0].get_text(" ", strip = True)

retail_positions = getIGsentiment('https://www.ig.com/us/forex/markets-forex/eur-usd')
print('EUR/USD:  ' + retail_positions)
您可以使用以下方法:

>>重新导入
>>>打印('EUR/USD:'+re.match('^.*在该市场上,'retail_positions.).group())
欧元/美元:57%的客户账户在该市场上短缺
您表示一个搜索模式(
^.*在这个市场上
),并且
re.match()
将返回一个
re.match
对象,您可以使用
group()
函数检索匹配项

此搜索模式由3部分组成:

  • ^
    匹配行首
  • *
    表示匹配任何字符(
    )的零个或多个(
    *
    )实例
  • 在这个市场上
    字面上匹配这个字符串

Regex被广泛使用和支持,但请注意一些变体,Python似乎不支持
[[:digit:]
字符类…

如果字符串发生了变化,但大写字母没有变化,您可以简单地创建一个for循环来处理第7个大写字符并拆分字符串。在这种情况下,是字母“T”

大概是这样的:

phrase = "EUR/USD:  57 % of client accounts are short on this market The percentage of 
IG client accounts with positions in this market that are currently long or short. 
Calculated to the nearest 1 % ."

upperchars = []
for char in phrase:
    if char.isupper():
        upperchars.append(char)

final = phrase.split(upperchars[6])[0]
print(final)
结果将是:

欧元/美元:57%的客户账户在该市场上短缺