正在尝试检索子字符串,但继续获取

2024-06-17 13:36:57 发布

您现在位置:Python中文网/ 问答频道 /正文

附件是一个小的网页抓取脚本。目标是在“|”字符中获取4个字母的“ticker symbol”。你知道吗

我试图返回子字符串“|”的位置,以便检索股票代码符号。我收到以下错误消息:

TypeError: 'method' Objection is not subscriptable

import bs4
import pandas as pd
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup


urls= ['https://www.macrotrends.net/stocks/charts/AAPL/apple/pe-ratio',
       'https://www.macrotrends.net/stocks/charts/MSFT/Microsoft/pe-ratio']

for url in urls:
  html = uReq(url)
  page_soup = soup(html, "html.parser")

  tickerTag = page_soup.find('title')
  print(tickerTag)
  tickerPOS = tickerTag.find["|"]
  print(tickerPOS)

我希望tickerPOS能回来。你知道吗

理想情况下,我尝试返回AAPL和MSFT的值。你知道吗


Tags: fromhttpsimportnethtmlaswwwurls
2条回答

首先需要让python将其解释为字符串,然后获取字符“|”的位置:

  ### ...
  tickerPOS = str(tickerTag).find("|")
  ### ...

链接中的结果32和36

在python中,函数参数放在括号中。拆下支架:

tickerTag.find("|")

相关问题 更多 >