简单的网页更改或删除按钮和删除数据是没用的

时间:2017-12-21 09:49:38

标签: python css selenium xpath web-scraping

我点了很多页面,用一个简单的按钮去除了刮擦,甚至只是页面中的一个小故障。

这个问题似乎出现了很多,但我不确定如何解决它。从本质上讲,随着团队,赔率和其他事情的消失,它会获得带有链接的xpath:( //*[contains(@class, "sport-block") and .//div/div]//*[contains(@class, "purple-ar")])。因为它应该但不是团队和可能造成无用的刮擦。

我最初使用的是CSS选择器,但我无法想象如何通过CSS的限制来实现这一点。

我想要的简单xpath:

//*[contains(@class, "sport-block") and .//div/div]//*[contains(@class, "purple-ar")]

问题仍然存在。

我对祖先和以前的人并不是很熟悉...但是像xpath这样的东西:

即://a/ancestor::div[contains(@class, 'xpath')]/preceding-sibling::div[contains(@class, 'xpath')]//a

为:

//a/ancestor::div[contains(@class, 'table-grid')]/preceding-sibling::span[contains(@class, 'sprite-icon arrow-icon arrow-right arrow-purple')]//a

可以解决(假设我可以让它工作)。

                        <td class="top-subheader uppercase">
                            <span>
                                English Premier League Futures
                            </span>
                        </td>
                    </tr>
                    <tr>
                        <td class="content">
                            <div class="titles">
                                <span class="match-name">
                                                                                <a href="/sports-betting/soccer/united-kingdom/english-premier-league-futures/outright-markets-20171226-616961-22079860">
                                            Outright Markets
                                        </a>
                                                                        </span>
                                <span class="tv">
                                                                                26/12

                                                                        </span>

                                                                        <span class="other-matches">
                                        <a href="/sports-betting/soccer/united-kingdom/english-premier-league-futures/outright-markets-20171226-616961-22079860" class="purple-arrow">5 Markets
                                            <span class="sprite-icon arrow-icon arrow-right arrow-purple"></span>
                                        </a>
                                    </span>

我有什么想法可以解决这个问题?感谢。

当前输出:

Steaua Bucharest    Link for below
Celtic  Link for below
Napoli  Link for below
Lyon    Link for below

所需:

Steaua Bucharest    LINK FOR Steaua Bucharest
Celtic  Link Celtic
Napoli  Link for Napoli
Lyon    Link for Lyon

enter image description here

我有什么想法可以解决这个问题?甚至缩小了做法?持续存在的问题。感谢。

1 个答案:

答案 0 :(得分:4)

为了确保每个组的数据结构完好无损,我遍历它们并使用嵌套(或相对的?我不确定这里的术语)XPath来获取数据。可以通过在每个查询之前放置.来使用相对XPath。

我也清理了一下:

  • 你抓住了一堆链接并用它们遍历页面直到完成。我用while循环替换了它。
  • 我添加了大量的try / except以尽可能多地捕获数据。
  • 我在每个新页面上添加了一个睡眠以允许加载数据(可以根据您的网络连接手动调整时间)。

如果这可以解决您的数据一致性问题,请告诉我。

import csv
import os
import time
from random import shuffle
from selenium import webdriver
from selenium.common.exceptions import TimeoutException, NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait

driver = webdriver.Chrome()
driver.set_window_size(1024, 600)
driver.maximize_window()

driver.get('https://crownbet.com.au/sports-betting/soccer')

header = driver.find_element_by_tag_name('header')
driver.execute_script('arguments[0].hidden="true";', header)
header1 = driver.find_element_by_css_selector('div.row.no-margin.nav.sticky-top-nav')
driver.execute_script('arguments[0].hidden="true";', header1)

# XPaths for the data
groups = '//div[@id="sports-matches"]/div[@class="container-fluid"]'
xp_match_link = './/span[@class="match-name"]/a'
xp_bp1 = './/div[@data-id="1"]//span[@class="bet-party"]'
xp_ba1 = './/div[@data-id="3"]//span[@class="bet-amount"]'
xp_bp3 = './/div[@data-id="3"]//span[@class="bet-party"]'
xp_ba3 = './/div[@data-id="3"]//span[@class="bet-amount"]'

while True:
    try:
        # wait for the data to populate the tables
        wait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, (xp_bp1))))
        time.sleep(2)

        data = []
        for elem in driver.find_elements_by_xpath(groups):
            try:
                match_link = elem.find_element_by_xpath(xp_match_link)\
                    .get_attribute('href')
            except:
                match_link = None

            try:
                bp1 = elem.find_element_by_xpath(xp_bp1).text
            except:
                bp1 = None

            try:
                ba1 = elem.find_element_by_xpath(xp_ba1).text
            except:
                ba1 = None

            try:
                bp3 = elem.find_element_by_xpath(xp_bp3).text
            except:
                bp3 = None

            try:
                ba3 = elem.find_element_by_xpath(xp_ba3).text
            except:
                ba3 = None

            data.append([match_link, bp1, ba1, bp3, ba3])
        print(data)

        element = driver.find_element_by_xpath('//span[text()="Next Page"]')
        driver.execute_script("arguments[0].scrollIntoView();", element)
        wait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, '//span[text()="Next Page"]')))
        element.click()

        with open('test.csv', 'a', newline='', encoding="utf-8") as outfile:
            writer = csv.writer(outfile)
            for row in data:
                writer.writerow(row)

    except TimeoutException as ex:
        pass
    except NoSuchElementException as ex:
        print(ex)
        break