使用BeautifulSoup爬取股票站点无法提取指定p标签内容如何解决
解决BeautifulSoup无法提取指定p标签内容的方案
- 问题根因:直接使用
find方法结合attrs匹配包含多个类名的标签时,需要完全匹配class属性的完整字符串(包括类名顺序、空格数量),只要页面实际的class属性和你填写的字符串有微小差异就会匹配失败,改用CSS选择器匹配即可兼容类名顺序、空格差异的场景。 - 修复代码:在你现有代码的末尾添加如下片段即可提取目标内容:
# 提取目标评级文本 rating_p = html.select_one('p.mt-3.pt-2.mb-0.rs-rel-085') if rating_p: rating_content = rating_p.text.strip() print(rating_content)
- 完整可运行代码如下:
from bs4 import BeautifulSoup as soup from urllib.request import Request, urlopen # Set up scraper url = "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] # 提取目标评级文本 rating_p = html.select_one('p.mt-3.pt-2.mb-0.rs-rel-085') rating_content = rating_p.text.strip() if rating_p else "未匹配到对应内容" # 输出所有字段 print(name) print(WKN) print(enterprise_value) print(PE,PS, div20, div19) print(perf_year, perf_hist, perf_avg) print(rating_content)
内容的提问来源于stack exchange,提问作者K - Town
相关产品推荐
相关产品推荐

