this is based on calsyslab project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
8.1 KiB

  1. __author__ = 'Helium'
  2. # Here, we are importing the auxiliary functions to clean or convert data
  3. from MarketPlaces.Utilities.utilities import *
  4. # Here, we are importing BeautifulSoup to search through the HTML tree
  5. from bs4 import BeautifulSoup
  6. # This is the method to parse the Description Pages (one page to each Product in the Listing Pages)
  7. def kingdom_description_parser(soup):
  8. # Fields to be parsed
  9. vendor = "-1" # 0 *Vendor_Name
  10. success = "-1" # 1 Vendor_Successful_Transactions
  11. rating_vendor = "-1" # 2 Vendor_Rating
  12. name = "-1" # 3 *Product_Name
  13. describe = "-1" # 4 Product_Description
  14. CVE = "-1" # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures)
  15. MS = "-1" # 6 Product_MS_Classification (Microsoft Security)
  16. category = "-1" # 7 Product_Category
  17. views = "-1" # 8 Product_Number_Of_Views
  18. reviews = "-1" # 9 Product_Number_Of_Reviews
  19. rating_item = "-1" # 10 Product_Rating
  20. addDate = "-1" # 11 Product_AddedDate
  21. BTC = "-1" # 12 Product_BTC_SellingPrice
  22. USD = "-1" # 13 Product_USD_SellingPrice
  23. EURO = "-1" # 14 Product_EURO_SellingPrice
  24. sold = "-1" # 15 Product_QuantitySold
  25. left = "-1" # 16 Product_QuantityLeft
  26. shipFrom = "-1" # 17 Product_ShippedFrom
  27. shipTo = "-1" # 18 Product_ShippedTo
  28. image = "-1" # 19 Product_Image
  29. vendor_image = "-1" # 20 Vendor_Image
  30. # Finding Product Name
  31. tag = soup.find('div', {"class": "col-md-9"})
  32. desc = tag.find('div',{"class": "col-md-8"}).find('div', {"class": "box-cont"})
  33. name = tag.find('div',{"class": "col-md-8"}).find('div', {"class": "box-head"}).text
  34. name = name.replace('\n', ' ')
  35. name = name.replace(',', ' ')
  36. name = name.strip()
  37. # Finding Prices
  38. # Kingdom prices can be shown in a variety of currencies, not all in USD, so keeping currency
  39. rows = desc.find_all('div', {"class", "row"}, recursive=False)
  40. price = rows[-1].find('div', {"class": "row"}).find('h3').text
  41. price = price.replace(',', '')
  42. price = price.strip()
  43. # USD = price.replace("USD",'')
  44. BTC = rows[-1].find('div', {"class": "row"}).find_next_sibling('div').find('span').text
  45. # Finding Vendor
  46. vendor = rows[0].select_one('a[href^="/user"]').text
  47. vendor = vendor.replace(",", " ")
  48. vendor = vendor.strip()
  49. # Finding Shipment Information (Origem)
  50. descs = rows[0].find_all('div', {"class": "col-md-3 text-right"})
  51. shipFrom = descs[2].text
  52. shipFrom = shipFrom.replace(",", "")
  53. shipFrom = shipFrom.strip()
  54. # Finding Shipment Information (Destiny)
  55. shipTo = rows[-1].find('div', {"class": "col-md-6"}).text
  56. shipTo = shipTo.replace("Ship to:","")
  57. shipTo = shipTo.replace(",","").strip()
  58. if(shipTo == ''):
  59. shipTo = -1
  60. # Finding the Product Category
  61. category = descs[0].text
  62. category = category.replace(",", "")
  63. category = category.strip()
  64. # Finding the Product Quantity Available
  65. left = descs[1].text
  66. left = left.replace(",", "")
  67. left = left.strip()
  68. # Finding when the Product was Added
  69. dt = descs[-1].text.strip()
  70. addDate = datetime.strptime(dt, '%d.%m.%Y')
  71. # Finding the Product description
  72. describe = cleanString(soup.find('div', {"id": "descriptionContent"}).text)
  73. # Finding the Number of Product Reviews
  74. review = len(soup.find('div', {"id": "feedbackContent"}).find_all(recursive=False))
  75. # Searching for CVE and MS categories
  76. # no cve or ms in Kingdom
  77. # Populating the final variable (this should be a list with all fields scraped)
  78. row = (vendor, rating_vendor, success, name, describe, CVE, MS, category, views, reviews, rating_item, addDate,
  79. BTC, USD, EURO, sold, left, shipFrom, shipTo, image, vendor_image)
  80. # Sending the results
  81. return row
  82. def kingdom_listing_parser(soup):
  83. # Fields to be parsed
  84. nm = 0 # *Total_Products (Should be Integer)
  85. mktName = "Kingdom" # 0 *Marketplace_Name
  86. vendor = [] # 1 *Vendor y
  87. rating_vendor = [] # 2 Vendor_Rating
  88. success = [] # 3 Vendor_Successful_Transactions
  89. name = [] # 4 *Product_Name y
  90. CVE = [] # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures)
  91. MS = [] # 6 Product_MS_Classification (Microsoft Security)
  92. category = [] # 7 Product_Category y
  93. describe = [] # 8 Product_Description
  94. views = [] # 9 Product_Number_Of_Views
  95. reviews = [] # 10 Product_Number_Of_Reviews
  96. rating_item = [] # 11 Product_Rating
  97. addDate = [] # 12 Product_AddDate
  98. BTC = [] # 13 Product_BTC_SellingPrice
  99. USD = [] # 14 Product_USD_SellingPrice y
  100. EURO = [] # 15 Product_EURO_SellingPrice
  101. sold = [] # 16 Product_QuantitySold
  102. qLeft =[] # 17 Product_QuantityLeft
  103. shipFrom = [] # 18 Product_ShippedFrom
  104. shipTo = [] # 19 Product_ShippedTo
  105. image = [] # 20 Product_Image
  106. image_vendor = [] # 21 Vendor_Image
  107. href = [] # 22 Product_Links
  108. listing = soup.find('div', {"id": "p0"}).find('div').find_all('div', {"class": "row"}, recursive=False)
  109. # Populating the Number of Products
  110. nm = len(listing)
  111. for a in listing:
  112. # Finding Prices
  113. #in array USD, there may be prices not in USD, so includes currency as well
  114. prices = a.find('div', {"class": "col-md-3"})
  115. u = prices.find('h3').text
  116. u = u.strip()
  117. u = u.replace(',', '')
  118. u = u.strip()
  119. USD.append(u)
  120. bc = prices.find('div').find('span').text
  121. BTC.append(bc)
  122. # Finding the Product
  123. product = a.find('div', {"class": "col-md-7"}).select_one('a[href^="/offer/view?"]').text
  124. product = product.replace('\n', ' ')
  125. product = product.replace(","," ")
  126. product = product.strip()
  127. name.append(product)
  128. # Finding Product Image
  129. product_image = a.find('img')
  130. product_image = product_image.get('src')
  131. product_image = product_image.split('base64,')[-1]
  132. image.append(product_image)
  133. # Finding the Vendor
  134. vendor_name = a.select_one('a[href^="/user"]').text
  135. vendor_name = vendor_name.replace(",", " ").replace('/', '')
  136. vendor_name = vendor_name.strip()
  137. vendor.append(vendor_name)
  138. image_vendor.append("-1")
  139. # Adding the url to the list of urls
  140. link = a.find('div', {"class": "col-md-7"}).select_one('a[href^="/offer/view?"]')['href']
  141. link = cleanLink(link)
  142. href.append(link)
  143. # Searching for CVE and MS categories
  144. # cve and ms not in kingdom
  145. # Populate the final variable (this should be a list with all fields scraped)
  146. return organizeProducts(mktName, nm, vendor, rating_vendor, success, name, CVE, MS, category, describe, views,
  147. reviews, rating_item, addDate, BTC, USD, EURO, sold, qLeft, shipFrom, shipTo, href,
  148. image, image_vendor)
  149. def kingdom_links_parser(soup):
  150. # Returning all links that should be visited by the Crawler
  151. href = []
  152. listing = soup.findAll('div', {"class": "col-md-7"})
  153. for a in listing:
  154. link = a.select_one('a[href^="/offer/view?"]')
  155. link = link['href']
  156. href.append(link)
  157. return href