私信  •  关注

Andersson

Andersson 最近创建的主题
Andersson 最近回复了
7 年前
回复了 Andersson 创建的主题 » 如何在xpath命令中使用python scrapy进行web抓取

你可以试试下面的变通方法:

'//*[starts-with(@id, "olnof_") and contains(@id, "_altlinesodd")]/tr[1]/TD[1]/A[1]'

ends-with(@id, "_altlinesodd") 在这种情况下,套房更好,但scrapy不支持 ends-with 语法,所以 contains 代替使用

如果只想让脚本遍历所有结果页,则不需要任何复杂的逻辑—只需单击“下一步”按钮,而这是可能的:

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
from selenium.common.exceptions import TimeoutException

driver = webdriver.Chrome()

driver.get('https://www.amazon.com/s/ref=nb_sb_noss_1?url=search-alias%3Daps&field-keywords=sonicare+toothbrush')

while True:
    try:
        wait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a > span#pagnNextString'))).click()
    except TimeoutException:
        break

另请注意 implicitly_wait(10) 不应该等 整整10秒 但是 等待10秒,使元素出现在html dom中 . 所以,如果元素在1或2秒内被找到,那么等待就完成了,您将不会等待剩下的8-9秒…

7 年前
回复了 Andersson 创建的主题 » python exec show错误:未定义名称

我不能复制你的问题,但我想你应该避免使用 exec() 在这种情况下。请尝试以下操作:

search_method = 'class name'
search_word = 'keyword'
elem_search_word = driver.find_element(search_method, search_word)
elem_search_word.send_keys('python')

如果你想搜索 id , xpath , css …刚刚设置 search_method 有适当的价值

或者:

from selenium.webdriver.common.by import By

search_method = By.CLASS_NAME
search_word = 'keyword'
elem_search_word = driver.find_element(search_method, search_word)
elem_search_word.send_keys('python')