英文:
automate the boring stuff - webscraping with selenium not working
问题
I'm trying my way through the book, but stuck on selenium here:
Link to the page in the book:
https://automatetheboringstuff.com/2e/chapter12/
To skip forward to the part with the code that is not working for me, look for:
"For example, open a new file editor tab and enter the following program: "
Now I realize that this code is not current as of 2023, but how would it have to look like to work? I've tried all afternoon with 2022/2023 tutorials found online, but no success. Thanks for any guidance you can give.
Per the website https://inventwithpython.com - if I use dev tools and element inspection + hover over "the recursive book of recursion," I see:
The book is only looking for "cover-thumb," I've also tried "card-img-top cover-thumb" and "card-img-top.cover-thumb."
Tried the following solutions from the web (amended to the case I'm looking for):
selenium-python.readthedocs.io
content = driver.find_element(By.CLASS_NAME, 'content')
elem = driver.find_element(By.CLASS_NAME, 'card-img-top.cover-thumb')
Tried this recent solution proposed on Stack overflow:
https://stackoverflow.com/questions/71989191/how-do-i-find-element-by-class-name-in-selenium
link_elements = find_elements(By.CLASS_NAME, "BM30N")
elem = find_elements(By.CLASS_NAME, "card-img-top cover-thumb") (also tried with a period instead of space)
even ChatGPT let me down - (Prompt: Tell me the python code to pull the class-name "card-img-top cover-thumb" via selenium from https://inventwithpython.com):
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://inventwithpython.com")
element = driver.find_element_by_class_name("card-img-top.cover-thumb")
class_name = element.get_attribute("class")
print(class_name)
driver.quit()
and of course looked at this too:
https://www.reddit.com/r/inventwithpython/comments/8ykq1i/automate_the_boring_stuff_with_python_corrections/
英文:
I'm trying my way through the book, but stuck on selenium here:
Link to the page in the book:
https://automatetheboringstuff.com/2e/chapter12/
To skip forward to the part with the code that is not working for me, look for:
"For example, open a new file editor tab and enter the following program: "
Now I realize that this code is not current as of 2023, but how would it have to look like to work? I've tried all afternoon with 2022/2023 tutorials found online, but no success. Thanks for any guidance you can give.
Per the website https://inventwithpython.com - if I use dev tools and element inspection + hover over "the recursive book of recursion", I see:
<img alt="Cover of The Recursive Book of Recusrion" src="images/cover_recursion_thumb.webp">
The book is only looking for " cover-thumb", I've also tried "card-img-top cover-thumb" and "card-img-top.cover-thumb"
Tried the following solutions from the web (amended to the case I'm looking for):
selenium-python.readthedocs.io
content = driver.find_element(By.CLASS_NAME, 'content')
elem = driver.find_element(By.CLASS_NAME, 'card-img-top.cover-thumb')
Tried this recent solution proposed on Stack overflow:
https://stackoverflow.com/questions/71989191/how-do-i-find-element-by-class-name-in-selenium
link_elements = find_elements(By.CLASS_NAME, "BM30N")
elem = find_elements(By.CLASS_NAME, "card-img-top cover-thumb") (also tried with a period instead of space)
even ChatGPT let me down - (Prompt: Tell me the python code to pull the class-name "card-img-top cover-thumb" via selenium from https://inventwithpython.com):
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://inventwithpython.com")
element = driver.find_element_by_class_name("card-img-top.cover-thumb")
class_name = element.get_attribute("class")
print(class_name)
driver.quit()
and of course looked at this too:
https://www.reddit.com/r/inventwithpython/comments/8ykq1i/automate_the_boring_stuff_with_python_corrections/
答案1
得分: 0
以下是您要翻译的内容:
For Chrome:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://inventwithpython.com")
element = driver.find_element(By.CLASS_NAME, "card-img-top.cover-thumb")
class_name = element.get_attribute("class")
print(class_name)
output:
card-img-top cover-thumb
For FireFox:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Firefox()
driver.get("https://inventwithpython.com")
element = driver.find_element(By.CLASS_NAME, "card-img-top.cover-thumb")
class_name = element.get_attribute("class")
print(class_name)
output:
card-img-top cover-thumb
英文:
Did you try this way:
For Chrome:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://inventwithpython.com")
element = driver.find_element(By.CLASS_NAME, "card-img-top.cover-thumb")
class_name = element.get_attribute("class")
print(class_name)
output:
card-img-top cover-thumb
For FireFox:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Firefox()
driver.get("https://inventwithpython.com")
element = driver.find_element(By.CLASS_NAME, "card-img-top.cover-thumb")
class_name = element.get_attribute("class")
print(class_name)
output:
card-img-top cover-thumb
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论