Answer a question

I am looking for some hints about http://lazada.co.th I have search links I need to scrape, but the results are inconsistent. Even manually Safari brings up different results from Chrome for the same link (number of items) For example https://www.lazada.co.th/shop-womens-sunglasses/?service=FS&location=local&price=5000-&rating=4 Shows me in Safari 31 items and in Chrome - 0

As for scraping I get totally different results from different approaches and from same approaches in time. I mean that BS4 and Selenium return different quantities and BS4 itself and Selenium itself are not consistent during even couple of hours. I can get results similar to Safari one time and similar to Chrome another time.

Playing with timeouts didn't help.

Any hints will be highly appreciated ))

Answers

The data is embedded within the page in Json form. You can use re/json module to extract it. The caveat is, that the page sometimes returns 0 results (this is completely random - I presume the coding is bad on their side).

This script will retry the loading until the data is available:

import json
import requests
from time import sleep


url = 'https://www.lazada.co.th/shop-womens-sunglasses/?service=FS&location=local&price=5000-&rating=4'

while True:
    txt = requests.get(url).text
    data = json.loads(re.search(r'window\.pageData=(.*?);?<', txt).group(1))

    if 'listItems' not in data['mods']:
        sleep(1)
        continue

    break

#uncomment next line to print all data:
#print(json.dumps(data, indent=4))

for i, item in enumerate(data['mods']['listItems'], 1):
    print('{:<3} {:>12} {:>15} {}'.format(i, item['price'], item['itemId'], item['name']))

Prints:

1           5390       390780925 Ray-Ban Square Sunglasses- RB1971 9150AC  แว่นตากันแดด
2        5390.00       390796431 Ray-Ban Square Sunglasses- RB1971 9149AD  แว่นตากันแดด
3       19290.00       100916363 GUCCI แว่นกันแดด รุ่น GG3660/K/S UD28ED/57
4       12000.00       275621098 Marc Jacobs แว่นตากันแดด รุ่น MJ457/F/S 35HEU ( Brown )
5       16900.00         7663524 Dior Mirrored Sunglasses- Rose Gold
6        9999.00       161253056 ic! berlin Malgorzata Rose Gold แว่นกันแดดกรอบสแตนเลส หรูหรา น้ำหนักเบาสุดๆ ของแท้ ฟรีแก้ว
7       38900.00       100133011 แว่นกันแดด DIOR SUN รุ่น silver matte peach/dark grey (RCM/BN)
8        7250.00        10928606 Ray-Ban Erika Polarized - RB4171F 710/T5 แว่นตากันแดด rayban
9       19990.00       100915722 GUCCI แว่นกันแดด รุ่น GG2245/S SJ5GXS/59
10      17890.00       100924756 GUCCI แว่นกันแดด รุ่น GG3754/F/S UKCLHA/58 (Black Black)
11      22790.00       101448214 GUCCI แว่นตากันแดด รุ่น GG3760/F/S U2ENHD/57
12      22790.00       100924670 GUCCI แว่นกันแดด รุ่น GG3727/F/S UILW6J/58 (Black Gold)
13      20290.00       100916203 GUCCI แว่นกันแดด รุ่น GG3635/N/F/S UZ9X0J/57
14       6990.00       100524937 GUESS แว่นกันแดด รุ่น GU7448 32B #52
15       5590.00       100525280 GUESS แว่นกันแดด รุ่น GU7460 M32B #60
16      22790.00       100924776 GUCCI แว่นกันแดด รุ่น GG3760/F/S U2EZHA/57 (Brown Black)
17      20290.00       101448302 GUCCI แว่นตากันแดด รุ่น GG4273/S S3YGMI/52
18      19990.00       100915356 GUCCI แว่นกันแดด รุ่น GG2245/S S006R3/61
19       5390.00       100516633 GUESS แว่นกันแดด รุ่น GU 6725 32C
20      18590.00       100924679 GUCCI แว่นกันแดด รุ่น GG3735/F/S UCHYUZ/57 (Red Brown)
21      26290.00       100883133 GUCCI แว่นตากันแดด GG2235/S SKJ1LG/59
22       5590.00       100525330 GUESS แว่นกันแดด รุ่น GU7460 M05B #60
23      20290.00       101443274 GUCCI แว่นตากันแดด GG3635/N/F/S UZ99HA/57
24      29790.00       101448179 GUCCI แว่นตากันแดด รุ่น GG3706/F/S U2ZXHA/58
25       7490.00       100524727 GUESS แว่นกันแดด รุ่น GU7434 02D #56
26       7200.00       100408876 Gucci   GG 3504/S D28/BN(Grey Black
27       6990.00       100524871 GUESS แว่นกันแดด รุ่น GU7448 02B #52
28       5390.00       100881696 GUESS แว่นกันแดด รุ่น GU6850 49G #54
29       6990.00       100525084 GUESS แว่นกันแดด รุ่น GU7448 09C #52
30       5390.00       100881586 GUESS แว่นกันแดด รุ่น GU6850 02B #54
31      27900.00         7765890 Dior Reflected Pink M2Q
Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐