如何使用GOOGLE的等待加载g-Load图标也在用PYTHON问硒?
问题描述
第一个。我非常感谢@Cruisepandey在这个话题上对我的帮助: How to crawl question and answer of Google People Also Ask with Selenium and Python?
所以我被这样使用他的代码:
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.implicitly_wait(30)
wait = WebDriverWait(driver, 30)
driver.get("https://www.google.com/search?q=How%20to%20make%20bakery%3F&source=hp&ei=j0aZYYjRAvja2roPrcWcyAU&iflsig=ALs-wAMAAAAAYZlUn4NMUPjfIpQmrXSmjIDnaWjJXWIJ&ved=0ahUKEwjI1JDn0Kf0AhV4rVYBHa0iB1kQ4dUDCAc&uact=5&oq=How%20to%20make%20bakery%3F&gs_lcp=Cgdnd3Mtd2l6EAMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBNQAFgAYJMDaABwAHgAgAF-iAF-kgEDMC4xmAEAoAECoAEB&sclient=gws-wiz")
all_questions = driver.find_elements(By.XPATH, "//span[text()='People also ask']/../following-sibling::div/descendant::div[@data-hveid and @class and @jsname and @data-ved]")
print(len(all_questions))
j = 1
for question in all_questions:
time.sleep(1)
ele = driver.find_element(By.XPATH, f"(//span[text()='People also ask']/../following-sibling::div/descendant::div[@data-hveid and @class and @jsname and @data-ved])[{j}]")
j = j + 2
ele.click()
time.sleep(1)
answer = ele.find_element(By.XPATH, ".//../following-sibling::div").get_attribute('innerText')
print(answer)
print('--------------')
这段代码非常有用。但我想问两个问题。
- 单击以显示答案时。如果没有使用时间。睡眠(1),我会使用等待答案显示,如何得到确切的类和代码来等待答案显示?
- 如果上网速度慢,代码会有问题。当单击更多时,将不会显示结果。我尝试使用等待,直到不可见性_元素_定位到加载图标。但我对XPath的理解不正确。有什么办法做到这一点吗? 这是我的代码更新@Cruisepandey:
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.implicitly_wait(30)
wait = WebDriverWait(driver, 30)
driver.get("https://www.google.com/search?q=How%20to%20make%20bakery%3F&source=hp&ei=j0aZYYjRAvja2roPrcWcyAU&iflsig=ALs-wAMAAAAAYZlUn4NMUPjfIpQmrXSmjIDnaWjJXWIJ&ved=0ahUKEwjI1JDn0Kf0AhV4rVYBHa0iB1kQ4dUDCAc&uact=5&oq=How%20to%20make%20bakery%3F&gs_lcp=Cgdnd3Mtd2l6EAMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBNQAFgAYJMDaABwAHgAgAF-iAF-kgEDMC4xmAEAoAECoAEB&sclient=gws-wiz")
all_questions = driver.find_elements(By.XPATH, "//span[text()='People also ask']/../following-sibling::div/descendant::div[@data-hveid and @class and @jsname and @data-ved]")
print(len(all_questions))
j = 1
for question in all_questions:
time.sleep(1)
ele = driver.find_element(By.XPATH, f"(//span[text()='People also ask']/../following-sibling::div/descendant::div[@data-hveid and @class and @jsname and @data-ved])[{j}]")
j = j + 2
ele.click()
# Question 1: To waiting answer show.
timeout = 30
answer_css_class = 'wWOJcd'
try:
is_question_show = WebDriverWait(driver, timeout).until(
EC.presence_of_element_located((By.CLASS_NAME, answer_css_class))
)
except TimeoutException:
pass
time.sleep(1)
answer = ele.find_element(By.XPATH, ".//../following-sibling::div").get_attribute('innerText')
# Question 2: To waiting G loading icon when click more answer with slow internet.
loading_element_xpath = '/html/body/div[7]/div/div[9]/div[1]/div/div[2]/div[2]/div/div/div[2]/div/div/div[1]/g-loading-icon'
try:
is_question_show = WebDriverWait(driver, timeout).until(
EC.presence_of_element_located((By.XPATH, loading_element_xpath))
)
except TimeoutException:
pass
print(answer)
print('--------------')
所以有什么办法不用时间吗?睡觉,然后在Selify中使用等待?
解决方案
我认为您不需要invisibility_of_element_located
来加载图标。
您可以单独尝试--问题和答案除外。
编码:
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
wait = WebDriverWait(driver, 30)
driver.get("https://www.google.com/search?q=How%20to%20make%20bakery%3F&source=hp&ei=j0aZYYjRAvja2roPrcWcyAU&iflsig=ALs-wAMAAAAAYZlUn4NMUPjfIpQmrXSmjIDnaWjJXWIJ&ved=0ahUKEwjI1JDn0Kf0AhV4rVYBHa0iB1kQ4dUDCAc&uact=5&oq=How%20to%20make%20bakery%3F&gs_lcp=Cgdnd3Mtd2l6EAMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBNQAFgAYJMDaABwAHgAgAF-iAF-kgEDMC4xmAEAoAECoAEB&sclient=gws-wiz")
all_questions = driver.find_elements(By.XPATH, "//span[text()='People also ask']/../following-sibling::div/descendant::div[@data-hveid and @class and @jsname and @data-ved]")
print(len(all_questions))
j = 1
for question in all_questions:
try:
ele = wait.until(EC.visibility_of_element_located((By.XPATH, f"(//span[text()='People also ask']/../following-sibling::div/descendant::div[@data-hveid and @class and @jsname and @data-ved])[{j}]")))
ele.click()
j = j + 2
except:
print("Could not click on Question link")
try:
answer = ele.find_element(By.XPATH, ".//../following-sibling::div").get_attribute('innerText')
print(answer)
except:
print("Could not read answer.")
导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
输出:
6
Follow the below-mentioned steps to open a successful bakery business in India in 2021:
Create A Bakery Business Plan. ...
Choose A Location For Your Bakery Business. ...
Get All Licenses Required To Open A Bakery Business In India. ...
Get Manpower Required To Open A Bakery. ...
Buy Equipment Needed To Start A Bakery Business.
More items...
A Detailed Guide On How To Start A Bakery Business In India
https://www.posist.com › Home › Resources
Search for: How do I start my own bakery?
The most profitable bakeries have a gross profit margin of 9%, while the average is much lower at 4%. The growth of profitable bakeries can be as high as 20% year over year. While a large number of bakeries never reach the break-even, a handful of them can even have a net profit margin as high as 12%.06-Jul-2020
How Much Do Bakery Owners Make? | Restaurant Accounting
https://restaurantaccounting.net › how-much-do-bakery-o...
Search for: Do bakeries make money?
Home Bakery Business - How to Start
Decide on the goods to bake. ...
Plan your kitchen space. ...
Get a permit. ...
Talk to a tax agent. ...
Set appropriate prices. ...
Start baking and selling.
16-Feb-2015
Home Bakery Business - How to Start | BakeCalc
http://www.bakecalc.com › blog › how-to-start-a-home-b...
Search for: How do I start a small baking business from home?
The success of any bakery, whether a home-based or commercial operation, hinges largely on the quality of the products. ... Creating a niche for your bakery, such as stunning cakes or unusual pastries, can help set you apart and build a loyal customer base.
What Makes a Home Bakery Business Successful?
https://smallbusiness.chron.com › home-bakery-business-s...
Search for: What makes a bakery successful?
Process finished with exit code 0
相关文章