File size: 3,713 Bytes
ead407c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import json
import re
import requests
from bs4 import BeautifulSoup


# url = "https://www.lightinthebox.com/en/p/_p9637820.html"
url = "https://www.lightinthebox.com/en/p/_p9528890.html"

resp = requests.get(url)

text = resp.text

print(resp.text)


pattern_without_rating = r"""
name":"(.*)","image":\[(?:.*)\],"brand":"(.*)","review":"(.*)","description":"(.*)","mpn":"(.*)","color":"(.*)","size":"(.*)","sku":"(.*?)".*,"offers":{"@type":"Offer","availability":"(?:.*)","priceCurrency":"(.*)","price":"(.*)","priceValidUntil":"(.*)","url
"""
pattern_without_rating = str(pattern_without_rating).strip()

pattern_review = r"""
aggregateRating":(?:.*)ratingValue":"(.*)","reviewCount":"(.*)"},"offers
"""
pattern_review = str(pattern_review).strip()

pattern_description = r"""
<div class="des-title-div">.*<h2 class="des-title">
"""
pattern_description = str(pattern_description).strip()


match = re.search(pattern_without_rating, text, flags=re.IGNORECASE)
title = match.group(1)
brand = match.group(2)
review = match.group(3)
description = match.group(4)
mpn = match.group(5)
# color = match.group(6)
# size = match.group(7)
sku = match.group(8)
priceCurrency = match.group(9)
price = match.group(10)
priceValidUntil = match.group(11)


match = re.search(pattern_review, text, flags=re.IGNORECASE)
if match is not None:
    ratingValue = match.group(1)
    reviewCount = match.group(2)
else:
    ratingValue = None
    reviewCount = None


soup = BeautifulSoup(text, features="html.parser")

matches = soup.find_all(class_="description-item")
result = list()
for match in matches:
    match_text = match.text.strip()
    match_text = str(match_text).strip().split("\n")
    for row in match_text:
        row = str(row).strip()
        if len(row) == 0:
            continue
        if row in ("Size Chart", "Photos"):
            break
        result.append(row)
overview = "\n".join(result)


matches = soup.find_all(class_="breadcrumbAB")
result = list()
for match in matches:
    match_text = match.text.strip()
    match_text = str(match_text).strip().split("\n")
    for row in match_text:
        row = str(row).strip()
        if len(row) == 0:
            continue
        result.append(row)
category = "".join(result)
category = " > ".join(category.split(">"))


match_select_color = soup.find("select", class_="select-color-show")
matches = match_select_color.find_all("option")
color = list()
for match in matches:
    match_text = match.text.strip()
    if match_text == "Color":
        continue
    color.append(match_text)


match_selects = soup.find_all("select", class_="attr-list-select")
color_size = [[], []]
color_size_idx = None
for match_select in match_selects:
    matches = match_select.find_all("option")
    for idx, match in enumerate(matches):
        match_text = match.text.strip()

        if idx == 0:
            if match_text == "Color":
                color_size_idx = 0
            elif match_text == "Size":
                color_size_idx = 1
            else:
                break
            continue

        color_size[color_size_idx].append(match_text)

color = color_size[0]
size = color_size[1]


row = {
    "title": title,
    "brand": brand,
    "review": str(review).strip() if len(str(review).strip()) != 0 else None,
    "description": description,
    "mpn": mpn,
    "color": color,
    "size": size,
    "sku": sku,
    "ratingValue": ratingValue,
    "reviewCount": reviewCount,
    "overview": overview,
    "category": category,
    "url": "https://www.lightinthebox.com/en/p/_p{}.html".format(mpn)

}
row = json.dumps(row, indent=4, ensure_ascii=False)
print(row)


if __name__ == '__main__':
    pass