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.

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