我有这样的代码:
url = requests.get('https://sneakernews.com/release-dates/').text
soup = BeautifulSoup(url, 'html.parser')
tags = soup.find_all('div', {'class': 'col lg-2 sm-3 popular-releases-box'})
for tag in tags:
link = tag.find('a').get('href')
tag1 = str(tag.text)
tag2 = tag1.splitlines()
print(tag2)
if link == 'javascript:void(0);':
link = 'Link not available'我有这样的输出:
['March ', 'OFF WHITE x Nike Air VaporMax']
['04.12 ', 'Nike Air VaporMax 97 "Silver Bullet"']
['April ', 'adidas Yeezy Desert Rat 500 "Blush"']
['05.15 ', 'ACRONYM x Nike Air VaporMax MOC 2']上面的输出是垂直的2个列表,我试着让每一行都成为一个列表。
发布于 2018-03-30 12:25:24
使用几个list comprehensions和zip(),可以做到如下所示:
代码:
the_tags = [[x.strip() for x in tag.text.splitlines()] for tag in tags]
result = [x for x in zip(*the_tags)]测试代码:
import requests
from bs4 import BeautifulSoup
url = requests.get('https://sneakernews.com/release-dates/').text
soup = BeautifulSoup(url, 'html.parser')
tags = soup.find_all('div', {'class': 'col lg-2 sm-3 popular-releases-box'})
the_tags = [[x.strip() for x in tag.text.splitlines()] for tag in tags]
print([x for x in zip(*the_tags)])结果:
[('March', '04.12', 'April', '05.15'),
('OFF WHITE x Nike Air VaporMax', 'Nike Air VaporMax 97 "Silver Bullet"',
'adidas Yeezy Desert Rat 500 "Blush"', 'ACRONYM x Nike Air VaporMax MOC 2'
)
]https://stackoverflow.com/questions/49568030
复制相似问题