Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.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的产品名称未保存在csv文件中_Python_Selenium_Web Scraping - Fatal编程技术网

硒+;Python数据抓取无法工作。Shopee的产品名称未保存在csv文件中

硒+;Python数据抓取无法工作。Shopee的产品名称未保存在csv文件中,python,selenium,web-scraping,Python,Selenium,Web Scraping,我的代码: from selenium import webdriver from time import sleep import csv from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium import webdriver

我的代码:

from selenium import webdriver
from time import sleep
import csv
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)

driver.get("https://shopee.ph/search?keyword=arduino&noCorrection=true&page=0&withDiscount=true")
sleep(2)

SCROLL_PAUSE_TIME = 0.5
last_height = driver.execute_script("return document.body.scrollHeight")

while True:
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    sleep(SCROLL_PAUSE_TIME)
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        break
    last_height = new_height
Categories = []
Categories.append(["NAME"])
driver.implicitly_wait(10)
items = driver.find_elements_by_xpath('//*[@id="main"]/div/div[3]/div/div[2]/div/div[2]/div')
for item in items:
    Name = item.find_element_by_xpath('.//*[@class="fBhek2 _2xt0JJ"]/div[2]/div/div').text
    print(Name)
    Categories.append(Name)
    sleep(1)
with open('Shopee.csv', 'a', encoding='utf-8') as file:
    Import = csv.writer(file,lineterminator='\n')
    Import.writerows(Categories)
因此,我试图使用Selenium和Pycharm来收集购物者的数据。首先是产品名称出现在运行控制台中,但出现在19 product之后。它会导致错误(如图所示)。第二,即使产品出现在符文控制台上。它们都没有被存放在里面。以下是我想收集的Shopee的链接:

我想知道怎么了?为什么它们都没有被储存在里面。然后,对于每19个产品名称显示,它会导致错误(我也在不同的产品搜索上尝试过,如笔记本电脑等)


首先,您应该改进定位器。
试试这个:

items = driver.find_elements_by_xpath('//div[@class="col-xs-2-4 shopee-search-item-result__item"]')
Name = item.find_element_by_xpath('.//div[@data-sqe="name"]').text
我还看到你第一次滚动到页面底部

while True:
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    sleep(SCROLL_PAUSE_TIME)
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        break
    last_height = new_height
只有在这之后,您才能尝试使用

items = driver.find_elements_by_xpath('//*[@id="main"]/div/div[3]/div/div[2]/div/div[2]/div')
for item in items:
    Name = item.find_element_by_xpath('.//*[@class="fBhek2 _2xt0JJ"]/div[2]/div/div').text
    print(Name)
    Categories.append(Name)
    sleep(1)

虽然您应该在滚动中获取产品数据,否则您将只获得屏幕下部显示的产品

error 404 not output error found,此社区不用于调试代码