【发布时间】:2020-07-20 03:13:47
【问题描述】:
网页抓取https://www.nike.com/w/mens-shoes-nik1zy7ok 获取鞋子。现在我可以使用以下代码检索最初加载的鞋子,以及滚动到下一页时加载的鞋子:
import re
import json
import requests
from bs4 import BeautifulSoup
url = 'https://www.nike.com/gb/w/womens-shoes-5e1x6zy7ok'
html_data = requests.get(url).text
data = json.loads(re.search(r'window.INITIAL_REDUX_STATE=(\{.*?\});', html_data).group(1))
for p in data['Wall']['products']:
print(p['title'])
print(p['subtitle'])
print(p['price']['currentPrice'], p['price']['currency'])
print(p['colorways'][0]['images']['portraitURL'].replace('w_400', 'w_1920'))
print('-' * 120)
next_page = data['Wall']['pageData']['next']
while next_page:
u = 'https://www.nike.com' + next_page
data = requests.get(u).json()
for o in data['objects']:
p = o['productInfo'][0]
print(p['productContent']['title'])
print(p['productContent']['subtitle'])
print(p['merchPrice']['currentPrice'], p['merchPrice']['currency'])
print(p['imageUrls']['productImageUrl'])
print('-' * 120)
next_page = data.get('pages', {'next':''})['next']
如何将所有这些鞋子附加在一起形成一个字典,我可以使用它来打印结果:
{% for shoe in shoes['Wall']['products'] %}
<p>{{shoe}}</p>
<h2>New shoe</h2>
{% endfor %}
【问题讨论】:
标签: python web-scraping beautifulsoup python-requests jinja2