Наиболее частая проблема: страница использует JavaScript для добавления элементов, но urllib
, requests
, BeautifulSoup
не может работать JavaScript.
Вы можете использовать Selenium или вы можете попытаться найти URL-адрес, используемый JavaScript для чтения данных с сервера.
Я нашел URL-адрес, используемый JavaScript для получения данных с сервера, и я использовал его с requests
, и я получил JSON данные, которые можно преобразовать в Python словарь / список
В этом словаре много данных, и теперь проблема состоит в том, чтобы найти правильные значения.
import requests
url = 'https://www.grainger.com/product/tableview/GRAINGER-APPROVED-Type-F-Stainless-Steel-Cam-WP11501162?breadcrumbCatId=1001429'
r = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
data = r.json()
for item in data['headers']:
print(item['title'])
for item in data['records'][0]['children']:
for x in item['techAttributes']:
print(' >', x['name'], '=', x['value'])
print('---')
Результат:
Coupling Size
Thread Size
Max. Working Pressure @ 70 F
Includes Locking Arm
Fitting Type
Item #
Price
> Coupling Size = 1/2"
> Thread Size = 1/2"-14
> Max. Working Pressure @ 70 F = 150 psi
> Includes Locking Arm = No
> Fitting Type = MNPT
> Item # = None
> Price = None
---
> Coupling Size = 3/4"
> Thread Size = 3/4"-14
> Max. Working Pressure @ 70 F = 150 psi
> Includes Locking Arm = No
> Fitting Type = MNPT
> Item # = None
> Price = None
---
> Coupling Size = 1"
> Thread Size = 1"-11-1/2
> Max. Working Pressure @ 70 F = 250 psi
> Includes Locking Arm = No
> Fitting Type = MNPT
> Item # = None
> Price = None
---
> Coupling Size = 1-1/4"
> Thread Size = 1-1/4"-11-1/2
> Max. Working Pressure @ 70 F = 250 psi
> Includes Locking Arm = No
> Fitting Type = MNPT
> Item # = None
> Price = None
---
> Coupling Size = 1-1/2"
> Thread Size = 1-1/2"-11-1/2
> Max. Working Pressure @ 70 F = 250 psi
> Includes Locking Arm = No
> Fitting Type = MNPT
> Item # = None
> Price = None
---
> Coupling Size = 2"
> Thread Size = 2"-11-1/2
> Max. Working Pressure @ 70 F = 250 psi
> Includes Locking Arm = No
> Fitting Type = MNPT
> Item # = None
> Price = None
---
> Coupling Size = 2-1/2"
> Thread Size = 2-1/2"-8
> Max. Working Pressure @ 70 F = 225 psi
> Includes Locking Arm = No
> Fitting Type = MNPT
> Item # = None
> Price = None
---
> Coupling Size = 3"
> Thread Size = 3"-8
> Max. Working Pressure @ 70 F = 200 psi
> Includes Locking Arm = No
> Fitting Type = MNPT
> Item # = None
> Price = None
---
> Coupling Size = 4"
> Thread Size = 4"-8
> Max. Working Pressure @ 70 F = 100 psi
> Includes Locking Arm = No
> Fitting Type = MNPT
> Item # = None
> Price = None
---
> Coupling Size = 5"
> Thread Size = 5"-8
> Max. Working Pressure @ 70 F = 75 psi
> Includes Locking Arm = No
> Fitting Type = MNPT
> Item # = None
> Price = None
---
> Coupling Size = 6"
> Thread Size = 6"-8
> Max. Working Pressure @ 70 F = 75 psi
> Includes Locking Arm = No
> Fitting Type = MNPT
> Item # = None
> Price = None
---