#我的代码:

url = 'http://wholepeople.com/best-thrift-stores-in-nyc/#:~:text=1%20Beacon%27s%20Closet.%20We%20think%20any%20thrifter%20in,...%2010%20Emma%20Rogue.%20...%20More%20items...%20'
page = urlopen(url)
soup = BeautifulSoup(page, 'html.parser')
h2Tags = soup.find_all('h2')
for store in h2Tags[:12]:
    allStores = (store.text)
    storeList.append(allStores)
    print(storeList)

#我得到的是:

['Beacon’s Closet']
['Beacon’s Closet', 'Laced Up']
['Beacon’s Closet', 'Laced Up', '2ND Street']
['Beacon’s Closet', 'Laced Up', '2ND Street', 'Buffalo Exchange']
['Beacon’s Closet', 'Laced Up', '2ND Street', 'Buffalo Exchange', 'Tired Thrift']
['Beacon’s Closet', 'Laced Up', '2ND Street', 'Buffalo Exchange', 'Tired Thrift', 'L Train Vintage']
['Beacon’s Closet', 'Laced Up', '2ND Street', 'Buffalo Exchange', 'Tired Thrift', 'L Train Vintage', 'Cure Thrift Shop']
['Beacon’s Closet', 'Laced Up', '2ND Street', 'Buffalo Exchange', 'Tired Thrift', 'L Train Vintage', 'Cure Thrift Shop', 'Crossroads Trading']
['Beacon’s Closet', 'Laced Up', '2ND Street', 'Buffalo Exchange', 'Tired Thrift', 'L Train Vintage', 'Cure Thrift Shop', 'Crossroads Trading', 'The Attic']
['Beacon’s Closet', 'Laced Up', '2ND Street', 'Buffalo Exchange', 'Tired Thrift', 'L Train Vintage', 'Cure Thrift Shop', 'Crossroads Trading', 'The Attic', 'Emma Rogue']
['Beacon’s Closet', 'Laced Up', '2ND Street', 'Buffalo Exchange', 'Tired Thrift', 'L Train Vintage', 'Cure Thrift Shop', 'Crossroads Trading', 'The Attic', 'Emma Rogue', 'Flamingo’s Vintage Pound']
['Beacon’s Closet', 'Laced Up', '2ND Street', 'Buffalo Exchange', 'Tired Thrift', 'L Train Vintage', 'Cure Thrift Shop', 'Crossroads Trading', 'The Attic', 'Emma Rogue', 'Flamingo’s Vintage Pound', 'Monk Vintage Clothing']

#我只需要最后一份 list ,不知道为什么/如何得到我所做的

推荐答案

try 此操作以获取最后一个列表:

url = 'http://wholepeople.com/best-thrift-stores-in-nyc/#:~:text=1%20Beacon%27s%20Closet.%20We%20think%20any%20thrifter%20in,...%2010%20Emma%20Rogue.%20...%20More%20items...%20'
page = urlopen(url)
soup = BeautifulSoup(page, 'html.parser')
h2Tags = soup.find_all('h2')
for store in h2Tags[:12]:
    allStores = (store.text)
    storeList.append(allStores)
print(storeList)

您每次都在打印列表,并且只想在生成列表后打印它.所以,只需将print语句移出循环.

Python相关问答推荐

有症状地 destruct 了Python中的regex?

Python,Fitting into a System of Equations

如何使用pytest来查看Python中是否存在class attribution属性?

如何在Raspberry Pi上检测USB并使用Python访问它?

组/群集按字符串中的子字符串或子字符串中的字符串轮询数据框

把一个pandas文件夹从juyter笔记本放到堆栈溢出问题中的最快方法?

python中字符串的条件替换

当递归函数的返回值未绑定到变量时,非局部变量不更新:

多处理队列在与Forking http.server一起使用时随机跳过项目

Python导入某些库时非法指令(核心转储)(beautifulsoup4."" yfinance)

实现神经网络代码时的TypeError

在嵌套span下的span中擦除信息

在两极中过滤

如何使用使用来自其他列的值的公式更新一个rabrame列?

在Docker容器(Alpine)上运行的Python应用程序中读取. accdb数据库

Pandas 数据帧中的枚举,不能在枚举列上执行GROUP BY吗?

合并相似列表

在一个数据帧中,我如何才能发现每个行号是否出现在一列列表中?

使用pythonminidom过滤XML文件

对列中的数字进行迭代,得到n次重复开始的第一个行号