ngh*_*ong 4 python selenium-webdriver
我尝试通过http://weixin.sogou.com抓取包含关键词的微信公众号
\n但我发现我必须使用两次 ActionChains(driver).move_to_element(nextpage).click().perform()\xef\xbc\x8cit 仍然可以工作,并转到下一页!
谁能告诉我原因以及如何解决!谢谢你!
\n源代码如下,抱歉注释是中文的。
\n# coding=utf-8\nimport time\nfrom selenium import webdriver\nfrom selenium.webdriver import ActionChains\nfrom selenium.webdriver.support.ui import WebDriverWait\nfrom selenium.webdriver.support import expected_conditions as EC\n\nkey = u"\xe6\xb1\x9f\xe5\x8d\x97\xe5\xa4\xa7\xe5\xad\xa6" #\xe6\x90\x9c\xe7\xb4\xa2\xe7\x9a\x84\xe5\x85\xb3\xe9\x94\xae\xe8\xaf\x8d\ndriver = webdriver.Chrome()\ndriver.get("http://weixin.sogou.com/")\nassert u\'\xe6\x90\x9c\xe7\x8b\x97\xe5\xbe\xae\xe4\xbf\xa1\' in driver.title\nelem = driver.find_element_by_id("upquery")\nelem.clear()\nelem.send_keys(key)\nbutton = driver.find_element_by_class_name("swz2") #\xe6\x90\x9c\xe7\xb4\xa2\xe5\x85\xac\xe4\xbc\x97\xe5\x8f\xb7\nbutton.click()\nWebDriverWait(driver,10).until(\n EC.title_contains(key)\n)\ncount = 0\nwhile True:\n for i in range(10):\n try:\n wechat_name = driver.find_element_by_xpath("//*[@id=\\"sogou_vr_11002301_box_{}\\"]/div[2]/h3".format(i)).text\n print wechat_name\n wechat_id = driver.find_element_by_xpath("//*[@id=\\"sogou_vr_11002301_box_{}\\"]/div[2]/h4/span/label".format(i)).text\n print wechat_id\n wechat_intro = driver.find_element_by_xpath("//*[@id=\\"sogou_vr_11002301_box_{}\\"]/div[2]/p[1]/span[2]".format(i)).text\n print wechat_intro\n print "*************************"\n count += 1\n except:\n pass\n try:\n nextpage = driver.find_element_by_xpath("//*[@id=\\"sogou_next\\"]") #\xe4\xb8\x8b\xe4\xb8\x80\xe9\xa1\xb5\xe7\x9a\x84\xe6\x8c\x89\xe9\x92\xae\n actions = ActionChains(driver)\n actions.move_to_element(nextpage)\n actions.click().\n actions.perform()\n actions = ActionChains(driver)\n actions.move_to_element(nextpage)\n actions.click().\n actions.perform()\n except Exception,e:\n print e\n break\ndriver.quit()\nprint count\nRun Code Online (Sandbox Code Playgroud)\n
您可以链接您的操作,因此无需在每个操作后执行。
actions = ActionChains(driver)
actions.move_to_element(nextpage)
actions.click(nextpage)
actions.perform()
Run Code Online (Sandbox Code Playgroud)
或者
actions = ActionChains(driver)
actions.move_to_element(nextpage)
actions.click(nextpage).perform()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9013 次 |
| 最近记录: |