|
|
|
|
|
import json |
|
import re |
|
import requests |
|
from bs4 import BeautifulSoup |
|
|
|
|
|
|
|
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) |
|
|
|
|
|
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 |
|
|