鱼C论坛

 找回密码
 立即注册
查看: 2666|回复: 1

[作品展示] 微信公众号分页爬取 python webdriver

[复制链接]
发表于 2017-3-8 11:05:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
使用webdriver爬取搜索到的微信公众号的信息,切实现分页爬取的功能:

=============================================================
import selenium, requests
from selenium import webdriver
import time, pymysql, sys, random, traceback
import os,time
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities


def ope_page(url):
        #chromedriver="D:\python\chromedriver_win32\chromedriver.exe"
        #os.environ["webdriver.chrome.driver"]=chromedriver
        #driver=webdriver.Chrome(chromedriver)
        phantomjs_path = r'D:\python\phantomjs-2.1.1-windows\bin\phantomjs.exe'
        driver = webdriver.PhantomJS(phantomjs_path)
        #driver = webdriver.PhantomJS(executable_path='D:\python\phantomjs-2.1.1-windows\bin\phantomjs.exe')
        driver.get(url)

                 
        text_body = driver.find_element_by_xpath('/html/body').text
   
#div_list=driver.find_element_by_xpath('//div[@class="news-box"]')

        for k in range(0,9):
                sogou_vr="sogou_vr_11002301_box_"+str(k)
                account_article="account_article_"+str(k)
                       
                t=driver.find_element_by_xpath("//li[@id='"+sogou_vr+"']//p[@class='tit']")
                i=driver.find_element_by_xpath("//li[@id='"+sogou_vr+"']//p[@class='info']")
                try:
                        a=driver.find_element_by_xpath("//li[@id='"+sogou_vr+"']//a[@uigs='"+account_article+"']")
                except:
                        print("文章缺失")
                        continue       
       
        #t=driver.find_element_by_xpath("//li[@id='%s']//p[@class='tit']"%sogou_vr)
        #i=driver.find_element_by_xpath("//li[@id='%s']//p[@class='info']"%sogou_vr)
        #a=driver.find_element_by_xpath("//li[@id='%s']//a[@uigs='%s']"%sogou_vr,%account_article)
                  
                title=t.text
                info1=i.text                          
                article=a.text
                user_name=i.find_element_by_xpath('//label[@name="em_weixinhao"]').text                          
                print(title)
                print(info1)
                print(user_name)
                print(article)
                  
                nowhandle=driver.current_window_handle
                          
                a.click()
                allhandle=driver.window_handles
                          
                for handle in allhandle:                          
                        if handle!=nowhandle:
                                driver.switch_to_window(handle)
                                time.sleep(3)
                                a3=driver.find_element_by_xpath('//h2[@id="activity-name"]')  
                        #print(article2)
                if article==a3.text:
                        print('true')
                else:
                        print('false')
                driver.switch_to_window(nowhandle)       
        #page=driver.find_element_by_xpath("//div[@id='pagebar_container']//a[@id='"+sogou_page+"']")
        #page.click()
    #return(driver.current_window_handle)
        driver.quit()          

def get_page_url(url,app_name,sogou_page,times):
        #chromedriver="D:\python\chromedriver_win32\chromedriver.exe"
        #os.environ["webdriver.chrome.driver"]=chromedriver
        #driver=webdriver.Chrome(chromedriver)
        phantomjs_path = r'D:\python\phantomjs-2.1.1-windows\bin\phantomjs.exe'
        driver = webdriver.PhantomJS(phantomjs_path)
        driver.get(url)

        driver.find_element_by_id("upquery").clear()
        driver.find_element_by_id("upquery").send_keys(app_name)
        driver.find_element_by_class_name("swz2").click()
       
        if times>1:
                page=driver.find_element_by_xpath("//div[@id='pagebar_container']//a[@id='"+sogou_page+"']")
                page.click()
        return driver.current_url
        driver.quit()
       
       

def main():
        url_t='http://weixin.sogou.com/'
        app_name_t='faker'
        for p in range(1,6):
                sogou_page_t = "sogou_page_"+str(p)
                url_value = get_page_url(url_t,app_name_t,sogou_page_t,p)
                print(url_value)
                ope_page(url_value)               


if __name__ == "__main__":
    main()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-3-8 11:34:47 | 显示全部楼层
乱七八糟的,说明都没有
复制黏贴一段代码就好了? 谁要看?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-28 09:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表