Код Python:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import Select
import pandas as pd
import time
# define the website to scrape and path where the chromedriver is located
website = 'https://www.adamchoi.co.uk/overs/detailed'
path = 'C:/users/Administrator/Downloads/chromedriver-win64/chromedriver.exe' # write your path here
service = Service(executable_path=path) # selenium 4
driver = webdriver.Chrome(service=service) # define 'driver' variable
# open Google Chrome with chromedriver
driver.get(website)
# locate and click on a button
all_matches_button = driver.find_element(by='xpath', value='//label[@analytics-event="All matches"]')
all_matches_button.click()
# select elements in the table
matches = driver.find_elements(by='xpath', value='//tr')
# storage data in lists
date = []
home_team = []
score = []
away_team = []
# looping through the matches list
for match in matches:
date.append(match.find_element(by='xpath', value='./td[1]').text)
home = match.find_element(by='xpath', value='./td[2]').text
home_team.append(home)
print(home)
score.append(match.find_element(by='xpath', value='./td[3]').text)
away_team.append(match.find_element(by='xpath', value='./td[4]').text)
# quit drive we opened at the beginning
driver.quit()
# Create Dataframe in Pandas and export to CSV (Excel)
df = pd.DataFrame({'date': date, 'home_team': home_team, 'score': score, 'away_team': away_team})
df.to_csv('football_data.csv', index=False)
print(df)
Как устранить следующее сообщение об ошибке, изменив приведенный выше код Python?
Traceback (most recent call last):
File "C:\Users\Administrator\PycharmProjects\WebScraping\1.selenium4-adamchoi.py", line 31, in
home = match.find_element(by='xpath', value='./td[2]').text
~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\PycharmProjects\WebScraping\.venv\Lib\site-packages\selenium\webdriver\remote\webelement.py", line 601, in find_element
return self._execute(Command.FIND_CHILD_ELEMENT, {"using": by, "value": value})["value"]
~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\PycharmProjects\WebScraping\.venv\Lib\site-packages\selenium\webdriver\remote\webelement.py", line 572, in _execute
return self._parent.execute(command, params)
~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\PycharmProjects\WebScraping\.venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 458, in execute
self.error_handler.check_response(response)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
File "C:\Users\Administrator\PycharmProjects\WebScraping\.venv\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 233, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"./td[2]"}
(Session info: chrome=142.0.7444.60); For documentation on this error, please visit: https://www.selenium.dev/documentation/ ... texception
Stacktrace:
Symbols not available. Dumping unresolved backtrace:
0x7ff6a9617a35
0x7ff6a9617a90
0x7ff6a93916ad
0x7ff6a93ea13e
0x7ff6a93ea44c
0x7ff6a93dcc9c
0x7ff6a93dcb56
0x7ff6a943b8fb
0x7ff6a93db068
0x7ff6a93dbe93
0x7ff6a98d29d0
0x7ff6a98cce50
0x7ff6a98ecc45
0x7ff6a96330ce
0x7ff6a963adbf
0x7ff6a9620c14
0x7ff6a9620dcf
0x7ff6a9606828
0x7ffb0b847bd4
0x7ffb0c6eced1
Process finished with exit code 1
Подробнее здесь: https://stackoverflow.com/questions/798 ... a-selenium
Сообщение об ошибке при очистке веб-сайта через селен ⇐ Python
Программы на Python
-
Anonymous
1762079150
Anonymous
Код Python:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import Select
import pandas as pd
import time
# define the website to scrape and path where the chromedriver is located
website = 'https://www.adamchoi.co.uk/overs/detailed'
path = 'C:/users/Administrator/Downloads/chromedriver-win64/chromedriver.exe' # write your path here
service = Service(executable_path=path) # selenium 4
driver = webdriver.Chrome(service=service) # define 'driver' variable
# open Google Chrome with chromedriver
driver.get(website)
# locate and click on a button
all_matches_button = driver.find_element(by='xpath', value='//label[@analytics-event="All matches"]')
all_matches_button.click()
# select elements in the table
matches = driver.find_elements(by='xpath', value='//tr')
# storage data in lists
date = []
home_team = []
score = []
away_team = []
# looping through the matches list
for match in matches:
date.append(match.find_element(by='xpath', value='./td[1]').text)
home = match.find_element(by='xpath', value='./td[2]').text
home_team.append(home)
print(home)
score.append(match.find_element(by='xpath', value='./td[3]').text)
away_team.append(match.find_element(by='xpath', value='./td[4]').text)
# quit drive we opened at the beginning
driver.quit()
# Create Dataframe in Pandas and export to CSV (Excel)
df = pd.DataFrame({'date': date, 'home_team': home_team, 'score': score, 'away_team': away_team})
df.to_csv('football_data.csv', index=False)
print(df)
Как устранить следующее сообщение об ошибке, изменив приведенный выше код Python?
Traceback (most recent call last):
File "C:\Users\Administrator\PycharmProjects\WebScraping\1.selenium4-adamchoi.py", line 31, in
home = match.find_element(by='xpath', value='./td[2]').text
~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\PycharmProjects\WebScraping\.venv\Lib\site-packages\selenium\webdriver\remote\webelement.py", line 601, in find_element
return self._execute(Command.FIND_CHILD_ELEMENT, {"using": by, "value": value})["value"]
~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\PycharmProjects\WebScraping\.venv\Lib\site-packages\selenium\webdriver\remote\webelement.py", line 572, in _execute
return self._parent.execute(command, params)
~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\PycharmProjects\WebScraping\.venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 458, in execute
self.error_handler.check_response(response)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
File "C:\Users\Administrator\PycharmProjects\WebScraping\.venv\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 233, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"./td[2]"}
(Session info: chrome=142.0.7444.60); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#nosuchelementexception
Stacktrace:
Symbols not available. Dumping unresolved backtrace:
0x7ff6a9617a35
0x7ff6a9617a90
0x7ff6a93916ad
0x7ff6a93ea13e
0x7ff6a93ea44c
0x7ff6a93dcc9c
0x7ff6a93dcb56
0x7ff6a943b8fb
0x7ff6a93db068
0x7ff6a93dbe93
0x7ff6a98d29d0
0x7ff6a98cce50
0x7ff6a98ecc45
0x7ff6a96330ce
0x7ff6a963adbf
0x7ff6a9620c14
0x7ff6a9620dcf
0x7ff6a9606828
0x7ffb0b847bd4
0x7ffb0c6eced1
Process finished with exit code 1
Подробнее здесь: [url]https://stackoverflow.com/questions/79806540/error-message-when-scraping-website-via-selenium[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия