我搜刮了一个股票列表,并将这些项目附加到一个列表中,但由于我的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查询,只获得一个股票列表?