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.

177 lines
8.3 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. __author__ = 'DarkWeb'
  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. # parses description pages, so takes html pages of description pages using soup object, and parses it for info it needs
  7. # stores info it needs in different lists, these lists are returned after being organized
  8. # @param: soup object looking at html page of description page
  9. # return: 'row' that contains a variety of lists that each hold info on the description page
  10. def quest_description_parser(soup):
  11. # Fields to be parsed
  12. vendor = "-1" # 0 *Vendor_Name
  13. success = "-1" # 1 Vendor_Successful_Transactions
  14. rating_vendor = "-1" # 2 Vendor_Rating
  15. name = "-1" # 3 *Product_Name
  16. describe = "-1" # 4 Product_Description
  17. CVE = "-1" # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures)
  18. MS = "-1" # 6 Product_MS_Classification (Microsoft Security)
  19. category = "-1" # 7 Product_Category
  20. views = "-1" # 8 Product_Number_Of_Views
  21. reviews = "-1" # 9 Product_Number_Of_Reviews
  22. rating_item = "-1" # 10 Product_Rating
  23. addDate = "-1" # 11 Product_AddedDate
  24. BTC = "-1" # 12 Product_BTC_SellingPrice
  25. USD = "-1" # 13 Product_USD_SellingPrice
  26. EURO = "-1" # 14 Product_EURO_SellingPrice
  27. sold = "-1" # 15 Product_QuantitySold
  28. left = "-1" # 16 Product_QuantityLeft
  29. shipFrom = "-1" # 17 Product_ShippedFrom
  30. shipTo = "-1" # 18 Product_ShippedTo
  31. image = "-1" # 19 Product_Image
  32. vendor_image = "-1" # 20 Vendor_Image
  33. # Finding Product Name
  34. name = soup.find('div', class_='card-header bg-dark text-white rounded-0 text-center').text
  35. name = cleanString(name).strip()
  36. # USD Price
  37. USD = soup.find('small', text='Product Price:').find_next('small').text.replace('$', '').strip()
  38. # Product Description
  39. describe = soup.find('textarea').text
  40. describe = cleanString(describe).strip()
  41. # Finding Product Image
  42. image = soup.find('img', {'class': 'img-fluid'})
  43. image = image.get('src').split('base64,')[-1]
  44. # Finding Vendor Image
  45. vendor_image = soup.select_one('.card-body.bg-mgray.css-selector.shadow img')
  46. vendor_image = vendor_image.get('src').split('base64,')[-1]
  47. # Finding Successful Transactions
  48. success = soup.find('strong', text='Total Sales:').parent.text
  49. success = cleanNumbers(success).strip()
  50. # Finding Vendor Rating
  51. temp = soup.find('strong', text='Rating:').parent
  52. rating_vendor = len(temp.findAll('i', {"class": "fas fa-star"}))
  53. half_stars = len(temp.findAll('i', {'class': "fas fa-star-half-alt"}))
  54. if half_stars > 0:
  55. rating_vendor += 0.5
  56. # Finding Item Rating
  57. temp = soup.find('small', text='Average Product Score:').find_next('small')
  58. rating_item = len(temp.findAll('i', {"class": "fas fa-star"}))
  59. half_stars = len(temp.findAll('i', {'class': "fas fa-star-half-alt"}))
  60. if half_stars > 0:
  61. rating_item += 0.5
  62. # Populating the final variable (this should be a list with all fields scraped)
  63. row = (vendor, rating_vendor, success, name, describe, CVE, MS, category, views, reviews, rating_item, addDate,
  64. BTC, USD, EURO, sold, left, shipFrom, shipTo, image, vendor_image)
  65. # Sending the results
  66. return row
  67. # parses listing pages, so takes html pages of listing pages using soup object, and parses it for info it needs
  68. # stores info it needs in different lists, these lists are returned after being organized
  69. # @param: soup object looking at html page of listing page
  70. # return: 'row' that contains a variety of lists that each hold info on the listing page
  71. def quest_listing_parser(soup):
  72. # Fields to be parsed
  73. nm = 0 # *Total_Products (Should be Integer)
  74. mktName = "quest" # 0 *Marketplace_Name
  75. vendor = [] # 1 *Vendor y
  76. rating_vendor = [] # 2 Vendor_Rating
  77. success = [] # 3 Vendor_Successful_Transactions
  78. name = [] # 4 *Product_Name y
  79. CVE = [] # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures) dont worry about this
  80. MS = [] # 6 Product_MS_Classification (Microsoft Security) dont worry about this
  81. category = [] # 7 Product_Category y
  82. describe = [] # 8 Product_Description
  83. views = [] # 9 Product_Number_Of_Views
  84. reviews = [] # 10 Product_Number_Of_Reviews
  85. rating_item = [] # 11 Product_Rating
  86. addDate = [] # 12 Product_AddDate
  87. BTC = [] # 13 Product_BTC_SellingPrice
  88. USD = [] # 14 Product_USD_SellingPrice y
  89. EURO = [] # 15 Product_EURO_SellingPrice
  90. sold = [] # 16 Product_QuantitySold
  91. qLeft = [] # 17 Product_QuantityLeft
  92. shipFrom = [] # 18 Product_ShippedFrom
  93. shipTo = [] # 19 Product_ShippedTo
  94. image = [] # 20 Product_Image
  95. image_vendor = [] # 21 Vendor_Image
  96. href = [] # 22 Product_Links
  97. # Extract all product listings
  98. listing = soup.findAll('div', class_='col-md-2 my-md-0 col-12')
  99. # Populating the Number of Products
  100. nm = len(listing)
  101. for a in listing:
  102. # Extracting Product URL & Name
  103. product_link_tags = a.find_all('a', class_='badge-info')
  104. if product_link_tags:
  105. # Using the first tag as default
  106. product_link_tag = product_link_tags[0]
  107. href.append(product_link_tag['href'])
  108. name.append(cleanString(product_link_tag.text).strip())
  109. # Extracting Product Image
  110. img_tag = a.find('img')
  111. if img_tag:
  112. image_data = img_tag['src'].split('base64,')[-1]
  113. image.append(image_data)
  114. # Extracting Vendor Name
  115. vendor_tag = a.find('a', class_='badge-dark')
  116. if vendor_tag:
  117. vendor.append(cleanString(vendor_tag.text.replace('👤', '')).strip())
  118. # Extracting Product Price in USD
  119. price_tag = a.find('a', class_='text')
  120. if price_tag:
  121. USD.append(price_tag.text.replace("$", "").strip())
  122. category_tag = soup.find('span', class_= 'btn btn-sm btn-outline-mgray active border-info')
  123. if category_tag:
  124. category.append(cleanString(category_tag.text).strip())
  125. # Populate the final variable (this should be a list with all fields scraped)
  126. return organizeProducts(mktName, nm, vendor, rating_vendor, success, name, CVE, MS, category, describe, views,
  127. reviews, rating_item, addDate, BTC, USD, EURO, sold, qLeft, shipFrom, shipTo, href, image, image_vendor)
  128. # called by the crawler to get description links on a listing page
  129. # @param: beautifulsoup object that is using the correct html page (listing page)
  130. # return: list of description links from a listing page
  131. def quest_links_parser(soup):
  132. # Returning all product links
  133. href = []
  134. # Locate all divs with class 'row'
  135. row_divs = soup.findAll('div', class_='row')
  136. for row_div in row_divs:
  137. # Locate all product divs within the current 'row' div
  138. product_divs = row_div.findAll('div', class_='col-md-2 my-md-0 col-12')
  139. for product_div in product_divs:
  140. # Locate the anchor tag containing the product link within each product div
  141. product_link_tag = product_div.find('a', class_='badge-info')
  142. if product_link_tag and product_link_tag.has_attr('href'):
  143. href.append(product_link_tag['href'])
  144. return href