无法使用BeautifulSoup从网站中刮取所有数据

2024-06-16 16:03:22 发布

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

我正试图从这个website中获取数据,但我无法从以下行中获取此特定信息:

"p class="mt-3 pt-2 mb-0 rs-rel-085"": "6,10 % aller Aktien sind besser bewertet.

(中文:“6.1%的股票评级更好。”)

我的代码适用于其余部分:

from bs4 import BeautifulSoup as soup
from urllib.request import Request, urlopen

# Set up scraper
url = (f"https://aktie.traderfox.com/visualizations/US30303M1027/DI/facebook-inc")
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
html = soup(webpage, "html.parser")

#find company name
name_1 = html.find("span",attrs={"class":"h1 m-0"})
name = name_1.text.strip()

#Ticker+WKN+ISIN
WKN_1 = html.find("span",attrs={"class":"color-grey2 d-lg-none"})
WKN = WKN_1.text.strip().replace("[","").replace("]","")

#enterprise value
value_2 = html.find("div",attrs={"class":"col-5 col-lg-auto d-lg-table-cell align-top text-nowrap"})
value_1 = value_2.find("td")
enterprise_value = value_1.text.strip()

#P/E, P/S, div. yield
fin_all = html.find_all("span",attrs={"class":"d-block d-sm-inline d-lg-block fs-rel-110"})
fin_pe = fin_all[0]
PE = fin_pe.text.strip()
fin_ps = fin_all[1]
PS = fin_ps.text.strip()
fin_div20 = fin_all[2]
div20 = fin_div20.text.strip()
fin_div19 = fin_all[3]
div19 = fin_div19.text.strip()

#Performance since year X and avg. return
perf3 = html.find_all("div",attrs={"class":"col-auto py-2 fs-080 color-grey2"})
perf2 = perf3[0]
perf1 = perf3[1]
perf_h = perf2.text.strip()
perf_d = perf1.text.strip()
perf_1 = html.find_all("div",attrs={"class":"col-auto py-2 fs-125 fs-lg-110 fs-xl-125"})
perf_2 = perf_1[0]
perf_hist = perf_2.text.strip()
perf_4 = perf_1[1]
perf_avg = perf_4.text.strip()
perf_year = perf_h[23:27]

print(name)
print(WKN)
print(enterprise_value)
print(PE,PS, div20, div19)
print(perf_year, perf_hist, perf_avg)

Tags: textnamevaluehtmlallfindfsattrs
1条回答
网友
1楼 · 发布于 2024-06-16 16:03:22

5,95根据通过单独的JSON请求获得的百分比分数计算得出。该值计算为100 - (100 * score)

from bs4 import BeautifulSoup
from urllib.request import Request, urlopen
from urllib import parse
import json

# Set up scraper
url = (f"https://aktie.traderfox.com/visualizations/US30303M1027/DI/facebook-inc")
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
soup = BeautifulSoup(webpage, "html.parser")

# << Your code here to get other items >>

# Locate the stock ID and request the JSON data for it
stock_id = soup.find('span', attrs={"data-id" : True})['data-id']
data = parse.urlencode({"stock_id" : stock_id}).encode()
req_fa =  Request("https://aktie.traderfox.com/ajax/getFactorAnalysis.php", data=data)
json_data = json.loads(urlopen(req_fa).read())

umsatzwachstum_growth = 100 - (100 * json_data["data"]["scores"]["salesgrowth5"]["score"])
eps_growth = 100 - (100 * json_data["data"]["scores"]["epsgrowth5"]["score"])
print(f"{umsatzwachstum_growth:.2f}, {eps_growth:.2f}")

这将给你:

5.95, 3.55

我建议您打印出json_data,以便更好地理解返回数据的格式

相关问题 更多 >