Я пытаюсь вытащить таблицу из этого https://www.worldometers.info/coronavirus/country/us/
Here is the code I am using
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
extension = r'cjpalhdlnbpafiamejdnhcphjbkeiagm.crx'
chrome_options = Options()
chrome_options.add_extension(extension)
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=r'chromedriver.exe')
url = 'https://www.worldometers.info/coronavirus/country/us/'
xpath = '//*[@id="usa_table_countries_today"]'
driver.get(url);
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, xpath))
)
except:
print("error")
driver.close()
finally:
element = driver.find_element_by_xpath(xpath)
element.screenshot_as_png("test.png")
driver.close()
I get the following error.
Traceback (most recent call last):
File "C:\Users\someUser\PycharmProjects\project\venv\lib\site-packages\urllib3\connection.py", line 160, in _new_conn
(self._dns_host, self.port), self.timeout, **extra_kw
File "C:\Users\someUser\PycharmProjects\project\venv\lib\site-packages\urllib3\util\connection.py", line 84, in create_connection
raise err
File "C:\Users\someUser\PycharmProjects\project\venv\lib\site-packages\urllib3\util\connection.py", line 74, in create_connection
sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
I also have attempted to use this code to get the table.
finally:
element = driver.find_element_by_xpath(xpath)
location = element.location;
size = element.size;
driver.save_screenshot("pageImage.png");
x = location['x'];
y = location['y'];
width = location['x'] + size['width'];
height = location['y'] + size['height'];
im = Image.open('pageImage.png')
im = im.crop((int(x), int(y), int(width), int(height)))
im.save('element_image.png')
driver.close()
But the above code gets the wrong section of the page.
введите описание изображения здесь
Чтобы устранить эту проблему, я также пытался с добавлением источника uBlock в селен и без него. В обоих случаях проблема не устранена.
Любой совет или помощь, которые помогут мне двигаться в правильном направлении, были бы очень признательны!