简体   繁体   English

网站上的文字未使用 selenium 抓取

[英]Text on website not scraping using selenium

I am trying to send text from a website that we order tires from into a discord embed.我正在尝试从我们订购轮胎的网站发送文本到 discord 嵌入。 I log into the site, search for an item and see that it is available.我登录该站点,搜索一个项目,然后看到它可用。 I want the discord embed to display the quantity that is available to order, but it just does not display anything.我希望 discord 嵌入显示可订购的数量,但它不显示任何内容。 Other elements it is displaying but not the Available , Alt Plant , Tire Tread and Est Delivery does not display.它显示的其他元素不显示AvailableAlt PlantTire TreadEst Delivery

(Apologies if my code is messy, I am new at this and I have been playing around with it for the last hour) (如果我的代码很乱,我深表歉意,我是新手,过去一个小时我一直在玩它)

# Finding the Quantity + Tire Description
    try:
        available1 = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.XPATH, "//*[@id='resultsTable']/tbody/tr[2]/td[9]"))
        )
        altPlant1 = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/div[2]/div[2]/div/form/div[1]/table/tbody/tr[2]/td[10]"))
        )
        estDelivery1 = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/div[2]/div[2]/div/form/div[1]/table/tbody/tr[2]/td[11]"))
        )
        tireSize1 = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/div[2]/div[2]/div/form/div[1]/table/tbody/tr[2]/td[5]"))
        )
        tireTread1 = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.XPATH, "//*[@id='resultsTable']/tbody/tr[2]/td[7]"))
        )
        itemCode1 = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/div[2]/div[2]/div/form/div[1]/table/tbody/tr[2]/td[2]"))
        )
    except:
        driver.quit()
    
    available = available1.text
    altPlant = altPlant1.text
    estDelivery = estDelivery1.text
    tireSize = tireSize1.text
    tireTread = tireTread1.text
    itemCode = itemCode1.text

    print(available)
    print(altPlant)
    print(estDelivery)
    print(tireSize)
    print(tireTread)
    print(itemCode)
    
# Discord Embed Setup   
    embed = Embed(
        description='**Stock available for item number '+pItemNumber+'**',
        color=0x0d0d22,
        timestamp='now'  # sets the timestamp to current time
        )

    embed.set_author(name='Pirelli Stock Check')
    embed.add_field(name='Item Number', value=itemCode, inline=True)
    embed.add_field(name='Tire Description', value=tireTread+' '+tireSize, inline=True)
    embed.add_field(name='Available', value=available+' In Stock', inline=False)
    embed.add_field(name='Alt Plant', value=altPlant+ ' In Stock', inline=True)
    embed.add_field(name='Est Delivery', value=estDelivery+'1', inline=True)
    embed.set_footer(text='Tires Tools', icon_url='https://cdn.discordapp.com/avatars/628005829840470037/8286685de0f2d7d94d94e020caf3265d.png?size=128')

    hook.send(embed=embed)

    print("Embed sent to discord!")

The 4 strings I need are in the source code too.我需要的 4 个字符串也在源代码中。 tireTread = CINTURATO P7 , available = 12 , altPlant = 7 and estDelivery = 08/24/2020 . tireTread = CINTURATO P7available = 12altPlant = 7estDelivery = 08/24/2020

4 rl

This is what comes through on my discord embed.这是通过我的 discord 嵌入得到的。

在此处输入图像描述

Any idea what I am doing wrong?知道我做错了什么吗? Let me know if you need more info.如果您需要更多信息,请告诉我。

You can find elements by their ID or CSS class:您可以通过 ID 或 CSS class 查找元素:

table = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID, "tabla_evolucion")))


table = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.class, "css-class sub-class")))

I recommend you find table by ID then read rows by looping.我建议您按 ID 查找表,然后循环读取行。 Maybe you need to change following code to your need.也许您需要根据需要更改以下代码。

table_id = self.driver.find_element(By.ID, 'table_id')
rows = table_id.find_elements(By.TAG_NAME, "tr") # get all of the rows in the table
for row in rows:
    # Get the columns (all the column 2)        
    col = row.find_elements(By.TAG_NAME, "td")[1] #note: index start from 0, 1 is col 2
    print col.text #prints text from the element

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM