我正在尝试使用R来运行我用python编写的函数。 那个函数使用R中的一些方法。所以当我从rPython导入函数时,它会崩溃,可能是由于循环导入。
基本上,我的R代码可以简化为:
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.keys import Keys
import csv
proxies = {
'http': 'http://31.173.209.111:8080',
'https': 'http://118.179.60.110:8080',
}
url = "https://www.amazon.com/Haggar-Hidden-Comfort-Waist-Plain/dp/B0018Q3BRO/ref=sr_1_3?ie=UTF8&qid=1518006207&sr=8-3&keywords=trousers+for+men"
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % proxies)
driver = webdriver.Chrome(executable_path="C:\\Users\Andrei\Downloads\chromedriver_win32\chromedriver.exe",
chrome_options=chrome_options)
driver.get(url)
product_title = driver.find_elements_by_xpath('//*[@id="productTitle"][1]')
product_price = driver.find_elements_by_xpath('//*[@id="priceblock_ourprice"][1]')
product_weight = driver.find_elements_by_xpath('//*[@id="detailBullets_feature_div"]/ul/li[2]/span/span')
product_dimension = driver.find_elements_by_xpath('//*[@id="detailBullets_feature_div"]/ul/li[1]/span/span')
product_asin = driver.find_elements_by_xpath('//*[@id="detailBullets_feature_div"]/ul/li[3]/span/span')
product_reviews = driver.find_elements_by_xpath('//*[@id="detailBullets_averageCustomerReviews"]/span/span')
prod_title = [x.text for x in product_title]
prod_price = [x.text for x in product_price]
prod_weight = [x.text for x in product_weight]
prod_dimension = [x.text for x in product_dimension]
prod_asin = [x.text for x in product_asin]
prod_reviews = [x.text for x in product_reviews]
print(prod_title[0])
print(prod_price[0])
print(prod_weight[1])
print(prod_dimension[1])
print(prod_asin[1])
try:
sizemenu = driver.find_element_by_id('dropdown_selected_size_name')
sizemenu.click()
select = driver.find_element_by_id('size_name_1') # medium size
select.click()
except:
print('no select')
sleep(3)
driver.find_element_by_xpath('//*[@id="submit.add-to-cart"]/span/input').click()
# driver.find_element_by_xpath('//*[@id="smartShelfAddToCartContinue"]/span/input')
sleep(3)
try:
driver.execute_script("document.getElementById('smartShelfAddToCartNative').click()")
except:
print('no continue button')
try:
driver.find_element_by_xpath('//*[@id="hlb-view-cart"]/span/a').click()
except:
print('no cart button')
sleep(3)
# items_cart = driver.find_element_by_xpath('//div[@class="a-alert-content"]/span')
driver.find_element_by_xpath('//*[@id="a-autoid-0-announce"]/span[2]').click()
driver.find_element_by_xpath('//*[@id="dropdown1_9"]').click()
quantity_xpath = '//*[@id="activeCartViewForm"]/div[2]/div/div[4]/div/div[3]/div/div/input'
quantity_el = driver.find_element_by_xpath(quantity_xpath)
quantity_el.send_keys("999" + Keys.ENTER)
sleep(2)
items_cart = driver.find_elements_by_xpath('//div[@class="a-alert-content"]/span')
items_in_cart = [x.text for x in items_cart]
csvfile='products_final.csv'
# Assuming res is a flat list
with open(csvfile, "w") as output:
writer = csv.writer(output, lineterminator='\n')
writer.writerow(items_in_cart[1])
导致R立即终止。
注意:上面的代码仅用于演示,在我的代码中我正在做一些有用的事情,例如:
library(rPython)
python.exec("import rpy2.robjects")
library(rPython)
python.exec("from my_package import foo")
位于import rpy2.robjects
。
我如何组织我的代码,以便我不会进行此循环导入?