从输出中删除额外的数据(html标签)?

0 人关注

我搜刮了一个股票列表,并将这些项目附加到一个列表中,但由于我的bs4查询,这样做也增加了额外的html元素。

Here is my reproducible code:

from bs4 import BeautifulSoup
from urllib.request import Request, urlopen
url = 'https://bullishbears.com/russell-2000-stocks-list/'
hdr = {'User-Agent': 'Mozilla/5.0'}
req = Request(url,headers=hdr)
page = urlopen(req)
soup = BeautifulSoup(page)
divTag = soup.find_all("div", {"class": "thrv_wrapper thrv_text_element"})
stock_list = []
for tag in divTag:
    strongTags = tag.find_all("strong")
    for tag in strongTags:
        for x in tag:      
            stock_list.append(x)

看一下列表的结果,我对股票字符串的格式很满意,每个股票(字符串的列表)后面都有一个逗号。正如你所看到的,我也得到了我想要删除的其他HTML元素<br/><span>

stock_list =

[<span data-css="tve-u-17078d9d4a6">RUSSELL 2000 STOCKS LIST</span>,
 <strong><strong><strong><span data-css="tve-u-17031e9c4ac"> We provide you a list of Russell 2000 stocks and companies below</span><span data-css="tve-u-17031e9c4ad">. </span></strong></strong></strong>,
 <strong><strong><span data-css="tve-u-17031e9c4ac"> We provide you a list of Russell 2000 stocks and companies below</span><span data-css="tve-u-17031e9c4ad">. </span></strong></strong>,
 <strong><span data-css="tve-u-17031e9c4ac"> We provide you a list of Russell 2000 stocks and companies below</span><span data-css="tve-u-17031e9c4ad">. </span></strong>,
 <span data-css="tve-u-17031e9c4ac"> We provide you a list of Russell 2000 stocks and companies below</span>,
 <span data-css="tve-u-17031e9c4ad">. </span>,
 'List of Russell 2000 Stocks & Updated Chart',
 'IWM',
 <br/>,
 'SPSM',
 <br/>,
 'VTWO',
 '/RTY',
 <br/>,
 '/M2K',
 'AAN',
 <br/>,
 'AAOI',
 <br/>,
 'AAON',
 <br/>,
 'AAT',
 <br/>,
 'AAWW',
 <br/>,
 'AAXN',
 <br/>,
 'ABCB',
 <br/>,
 'ABEO',
 <br/>,
 'ABG',
 <br/>,
 'ABM',
 <br/>,
 'ABTX',
 <br/>,
 'AC',
 <br/>,
 'ACA',
 <br/>,
 'ACAD',
 <br/>,
 'ACBI',
 <br/>,
 'ACCO',
# More to the list but for brevity I removed the rest.

我怎样才能正确地微调我的bs4查询,只获得一个股票列表?

2 个评论
试着在你的代码中添加 [str(elem) for elem in stock_list if not str(elem).startswith('<')] 到循环之后。
谢谢你,这对我很有帮助,我确实还有4个散兵游勇,但它们很容易找到并自己删除。
python
beautifulsoup
andres
andres
发布于 2020-12-21
1 个回答
uingtea
uingtea
发布于 2020-12-21
已采纳
0 人赞同

你需要拆分数值,因为多个股票在 strong 标签内。

<strong>AAN<br>AAOI<br>AAON<br>AAT<br>....</strong>

the code

# better and easier using CSS selector
strongTags = soup.select('.tcb-col .thrv_wrapper.thrv_text_element strong')
stock_list = []
for s in strongTags:
    # .decode_contents() to get innerHTML
    stocks = s.decode_contents().split('<br/>');