zoukankan      html  css  js  c++  java
  • Requests-html解析库的使用

    Requests-html解析库的使用

    常见的解析库:re正则,bs4,pyppeteer(驱动浏览器内核),lxml
        
    安装:pip install requests-html    
        
    from requests_html import HTMLSession,HTML
    
    #可视化设置
    """
    session = HTMLSession(
        browser_args=[
        '--no-sand',
        '--user-agent=XXXXX',
        ],headless=False
    )
    """
    
    
    session = HTMLSession() 
    response = session.request() 与requests响应基本一样
    
    #解析response.html的属性:
    
    response.html.absolute_links	(返回页面中所有路径为绝对路径)
    
    			 .links			  (页面中的所有路径)
    
    			 .base_url		   (页面中base标签的路径)
    
    			 .html			   (解析页面布局内容)
    
    			 .text			   (获得页面标签中的所有文本)
    
    			 .encoding = 'gbk'	(r.html的编码方式)
    
    			  .raw_html			(二进制页面内容)
    
    			  .pq				(pyQuery)
    
                
    #解析response.html的属性方法:
    r.html.find('css选择器')	:返回[element对象1,...]
        
          .find('css选择器',first=True)
    	  
          .xpath('xpath选择器',first=True)
            
     
    #response.html的search方法
    result=r.html.search(''<a href="{src}">{name}</a>'')
    返回页面由上至下的第一个Result对象,通过reslut['name']方式取出值
    
    result=r.html.search_all(''<a href="{src}">{name}</a>'')
    返回页面上[Result对象1,....]
    
    
    #response.html的render方法
    response.html.render()
    	参数:
            js注入:
                script = """
                		var a = document.querySelector("#query")
                		var b = a.getBoundingClientRect()
                		return {"x":b.x}
                		"""
            scrolldown = 10	向下滑动
    	    sleep = 10 
            keep_page = False(默认为False)
            当设置为True时,阻止关闭浏览器,通过r.html.page与浏览器进行交互        	            
            如何绕过网站对webdriver的检测:(可反爬)
                script='''() =>{
                                Object.defineProperties(navigator,{
                                webdriver:{
                                        get: () => undefined
                                        }
                                    })
                                }'''
    
            
    #Element对象的方法
    element=r.html.find('css选择器',first=True)
    
    element.absolute_links
    	   .links
    	   .text
    	   .html
    	   .attrs(返回属性的集合)
    	   .find('css选择器')
    	   .search('模板')
    	   .search_all('模板')
    
    
    #r.html.page与浏览器进行交互  
    首先,r.html.render(keep_page = True)
    async def run():
        #交互语句
         mydic = await r.html.page.evaluate('''() =>{
         var a = document.querySelector('#kw')
         var b = a.getBoundingClientRect()
         return {'x':b.x,'y':b.y , 'width':b.width , 'height':b.height }
         }''')
    	 screenshot截图,path:截图文件保存地址,clip:四个参数
         #await r.html.page.screenshot({'path':'1.png','clip':mydic})
         
         type('css选择器','内容',{'delay':100}),模拟输入框内容  
        #await r.html.page.type('#kw','东京热',{'delay':400})
            
        hover 模拟鼠标悬浮   
       	#await r.html.page.hover('li[class="category-item"]')
        
        focus模拟鼠标光标聚焦
        # await r.html.page.focus('#login_field')
        # await r.html.page.keyboard.type('935938484@qq.com')
        # await r.html.page.focus('#password')
        # await r.html.page.keyboard.type('a-new-world')
        
        模拟键盘事件
        # await r.html.page.click('[name="commit"]',{'button':'left'})
        #await r.html.page.keyboard.type('喜欢你啊aaaaa')
        #await r.html.page.keyboard.down('Shift')
        #for i in range(5):
        #    await r.html.page.waitFor(100)
        #    await r.html.page.keyboard.press('ArrowLeft')
        #await r.html.page.keyboard.up('Shift')
        #await r.html.page.keyboard.press('Backspace')
        #await r.html.page.waitFor(5000)
        
        模拟点击事件
        .click(x,y,{
                    'button':'left',
                    'click':1
                    'delay':0
    			})
    			.down({'button':'left'})
    			.up({'button':'left'})
    			.move(x,y,{'steps':1})
    
    
    try:
        session.loop.run_until_complete(run())
    finally:
        session.close() 
    
  • 相关阅读:
    二分练习题4 查找最接近的元素 题解
    二分练习题5 二分法求函数的零点 题解
    二分练习题3 查找小于x的最大元素 题解
    二分练习题2 查找大于等于x的最小元素 题解
    二分练习题1 查找元素 题解
    code forces 1176 D. Recover it!
    code forces 1173 B. Nauuo and Chess
    code forces 1173 C. Nauuo and Cards
    吴恩达深度学习课程笔记-15
    吴恩达深度学习课程笔记-14
  • 原文地址:https://www.cnblogs.com/bruce123/p/11695141.html
Copyright © 2011-2022 走看看