Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Shopee产品名称中的数据刮削未分别显示其正确名称_Python_Selenium_Selenium Webdriver - Fatal编程技术网

Python Shopee产品名称中的数据刮削未分别显示其正确名称

Python Shopee产品名称中的数据刮削未分别显示其正确名称,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,因此,我尝试使用Pycharm/Python和Selenium在Shopeee中刮取数据。代码如下: 从selenium导入webdriver 导入时间 导入csv 从selenium.webdriver.common.keys导入密钥 从selenium.webdriver.common.by导入 从selenium.webdriver.support.ui导入WebDriverWait 从selenium导入webdriver PATH=“C:\ProgramFiles(x86)\chrom

因此,我尝试使用Pycharm/Python和Selenium在Shopeee中刮取数据。代码如下:

从selenium导入webdriver
导入时间
导入csv
从selenium.webdriver.common.keys导入密钥
从selenium.webdriver.common.by导入
从selenium.webdriver.support.ui导入WebDriverWait
从selenium导入webdriver
PATH=“C:\ProgramFiles(x86)\chromedriver.exe”
driver=webdriver.Chrome(路径)
驱动程序。获取(“https://shopee.ph/search?keyword=nacific&noCorrection=true&page=0&withDiscount=true")
时间。睡眠(2)
执行_脚本(“window.scrollTo(0,document.body.scrollHeight)”)
时间。睡眠(3)
类别=[]
类别。追加([“名称”])
驱动程序。隐式等待(10)
products=driver.find_elements_by_xpath(“//div[@class='row shopee-search-item-result_uitems']/div”)
对于产品中的产品:
name_p=product.find_element_by_xpath(“//div[@class='yQmmFK _1POlWt _36CEnF']))
rowData=[name\u p]
Categories.append(行数据)
打开('Shopee.csv','a',encoding='utf-8')作为文件:
Import=csv.writer(文件,行终止符='\n')
Import.writerows(类别)
所以在我运行它之后…我“成功”运行了它,但问题是:

它不显示产品名称,而是显示selenium.webdriver等 我试图通过不使用xpath和常规方式(通过类名称等查找元素)来更改为其他代码,但仍然会导致错误。我想知道为什么它不起作用?有人能帮我吗

网站我正在努力刮刮
:Shopee.ph
软件:Pycharm和Selenium

您可以使用
.text
从Selenium WebElements获取文本

范例

product.find_element_by_xpath("//div[@class='yQmmFK _1POlWt _36CEnF']").text
有很多方法可以定位web元素

element = driver.find_element_by_id("passwd-id")
element = driver.find_element_by_name("passwd")
element = driver.find_element_by_xpath("//input[@id='passwd-id']")
element = driver.find_element_by_css_selector("input#passwd-id")
你可以在这里找到文件。

我还修复了你的一些代码。这里

products = driver.find_elements_by_class_name('yQmmFK')

for product in products:
    name_p = product.text
    rowData = [name_p]
    Categories.append(rowData)
driver.close()

与其写
rowData=[name\u p]
try
rowData=[name\u p.text]
name\u p
是一个网页元素,而不是字符串。嗨,C.Peck,我试过了,是的,它终于起作用了,但问题是,产品的名称……或者更确切地说,产品只显示了一个产品:“NACIFIC Nutrition Herb Origin Serum 50ml 1+1”从第1列开始,依此类推。只有一个产品名称,而不是Shopee页面的所有产品。我想知道为什么?假设其意图是将网站上的所有产品都展示出来。但它只使用了第一个产品,然后将其完全重复到excel的每个列单元格中。。。