Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

python - 如何在循环中附加每个项目数据(how to append each item data in loop [duplicate])

I can get data"name","like","price".But after clicking each item link,I can't get item details.

(我可以获得数据“名称”,“喜欢”,“价格”。但是单击每个项目链接后,我无法获得项目详细信息。)

It gives a stale element reference: element is not attached to the page document error.

(它提供了一个过时的元素引用:元素未附加到页面文档错误。)

I have no idea how to append each item data in loop.

(我不知道如何在循环中附加每个项目数据。)

# coding: UTF-8
from time import sleep
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import csv

import sys
args = sys.argv


if __name__ == '__main__':
items_list = []


options = Options()
options.add_argument('--headless')


driver = webdriver.Chrome(executable_path="", chrome_options=options)

query = args[1]

driver.get("https://www.mercari.com/jp/search/?sort_order=&keyword=&category_root=&brand_name=&brand_id=&size_group=&price_min=&price_max=&status_trading_sold_out=1".format(query))
    sleep(1)

    page = 1

    next_page = driver.find_elements_by_css_selector("body > div.default-container > main > div.l-content > ul > li.pager-next.visible-pc > ul > li:nth-child(1) > a")

    while True:
        sleep (3)
        if len(next_page) > 0:

            items =  driver.find_elements_by_xpath("/html/body/div[1]/main/div[1]/section/div[2]/section")
            for i in range(0,len(items)):
                sleep(3)
                name = items[i].find_element_by_xpath("a/div/h3").text
                like = items[i].find_element_by_xpath("a/div/div/div[2]/span").text
                price = items[i].find_element_by_xpath("a/div/div/div[1]").text

                items[i].find_element_by_xpath("a").click()

                sleep(1)

                user = driver.find_element_by_xpath("/html/body/div[1]/section/div[1]/table/tbody/tr[1]/td/a").text
                rate = driver.find_element_by_xpath("/html/body/div[1]/section/div[1]/table/tbody/tr[1]/td/div/div[1]/span").text

          items_list.append([name,like,price,user,sold])

                driver.back()

            page+=1

            btn = driver.find_element_by_css_selector("li.pager-next .pager-cell:nth-child(1) a").get_attribute("href")
            driver.get(btn)
            print("Moving to next page......")

        else:
            print("no pager exist anymore")
            break

    driver.close()
    driver.quit()

I added code which make it easier to understand for you guys.

(我添加了代码,使您更容易理解。)

Thank you.

(谢谢。)

  ask by tomoki translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...