Guest User

Untitled

a guest
Jul 20th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. import csv
  2. from selenium import webdriver
  3. from bs4 import BeautifulSoup
  4. import requests
  5. from lxml import html
  6. import io
  7.  
  8. links = [
  9. 'https://www.amazon.com/s/ref=sr_pg_1?fst=as%3Aoff&rh=n%3A1055398%2Cn%3A1063306%2Ck%3Aas&keywords=as&ie=UTF8&qid=1532070774'
  10. ]
  11. proxies = {
  12. 'http': 'http://218.50.2.102:8080',
  13. 'https': 'http://185.93.3.123:8080'
  14. }
  15.  
  16. chrome_options = webdriver.ChromeOptions()
  17.  
  18. chrome_options.add_argument('--proxy-server="%s"' % ';'.join(['%s=%s' % (k, v) for k, v in proxies.items()]))
  19.  
  20. driver = webdriver.Chrome(executable_path="C:\UsersAndrei-PCDownloadswebdriverchromedriver.exe",
  21. chrome_options=chrome_options)
  22. header = ['Product title', 'Product price', 'Review', 'ASIN']
  23.  
  24. with open('csv/demo.csv', "w") as output:
  25. writer = csv.writer(output)
  26. writer.writerow(header)
  27. for i in range(len(links)):
  28.  
  29. driver.get(links[i])
  30. for x in range(0,23):
  31. product_title = driver.find_elements_by_xpath('//li[@id="result_{}"]/div/div[3]/div/a'.format(x))
  32. title = [x.text for x in product_title]
  33.  
  34. try:
  35. price = driver.find_element_by_xpath('//li[@id="result_{}"]/div/div[5]/div/a/span[2]'.format(x)).text
  36. except:
  37. price = 'No price v2'
  38. print('No price v2')
  39.  
  40. try:
  41. review = driver.find_element_by_xpath('//li[@id="result_{}"]/div/div[6]/span'.format(x)).text()
  42.  
  43. except:
  44. review = 'No review v1'
  45. print('No review v1')
  46.  
  47. try:
  48. asin = driver.find_element_by_id('result_{}'.format(x)).get_attribute('data-asin')
  49.  
  50. except:
  51. asin = 'No asin'
  52. print('No asin')
  53.  
  54. try:
  55. data = [title[0], price, review, asin]
  56. except:
  57. print('no items v3 ')
  58. with io.open('csv/demo.csv', "a", newline="", encoding="utf-8") as output:
  59. writer = csv.writer(output)
  60. writer.writerow(data)
  61. print('I solved this link %s' % (links[i]))
  62. print('Number of product %s' % (i + 1))
  63.  
  64. stars = driver.find_element_by_class_name('a-icon-alt').get_attribute('textContent')
Add Comment
Please, Sign In to add comment