爬虫常用库

1.requests

1.获取某一个网页
    requests.get(url)
    请求的参数可以设置headers等。
2.各种请求
    requests.post(url,data = {})
    requests.delete(url)
    requests.head(url)
    requests.options(url)
3.获取网页cookie
    response = requests.get(url)
    response.cookies
    
    for key,value in response.cookies.items():
        print(key + "="+value)
    让然获取的从cookie是可以加入到请求的(url,cookies = cookies)
    一般也会使用cookie jar
    jar = requests.cookies.RequestsCookieJar()
    jar.set('tasty_cookie', 'yum', domain='httpbin.org', path='/cookies')
    jar.set('gross_cookie', 'blech', domain='httpbin.org', path='/elsewhere')
    response = requests.get(url, cookies=jar)
    print(response.text)

这是使用的比较多的,使用较少的就不列举了,参考文档:https://www.jianshu.com/p/ecb4d54ad8cf

2.selenium

selenium:一个web自动化测试工具,直接运行在浏览器上
1、导包
    from selenium import webdriver
2.加载页面
    driver = webdrive.Chrome()
3.页面元素的查找
    1.获取id标签值
        driver.find_element_by_id()
    2.获取name标签值
        driver.find_element_by_name()
    3.获取标签名值
        driver.find_elements_by_tag_name()
    4.通过xpath匹配(找一个)
        driver.find_element_by_xpath()
    5.找所有
        driver.find_elements_by_xpath()
    6.通过css定位
        driver.find_element_by_css_selector()
    7.向下滚动到页面底部
        driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')
    8.关闭当前所在窗口和退出浏览器
        driver.close()
        driver.quit()

3.lxml

from lxml import etree
html = etree.HTML(text)

  

4.beautiful soup  

beautifulsoup对象
主要的功能也是网页抓取数据,Beautifulsoup自动将输入的文档转换为Unicode编码,输出文档转换为utf-8编码。一般beautifulsoup和lxml一起使用
    from bs4 import BeautifulSoup
    import lxml
    import requests
    wb_data = requests.get(url)
    soup = BeautifulSoup(wb_data.text,'lxml')
步骤:
    1.导入bs4,lxml,requests
    2.创建BeautifulSoup对象
        soup = BeautifulSoup(html,'lxml')
    3.打印soup对象的内容
        soup.prettify()格式化输出
    4.获取页面信息的四种对象
        1.Tag(两个重要的属性name,attrs)
            tag为html中的一个个标签
            soup.title,soup.head,soup.a,soup.p等    
        2.NavigableString标签
            得到了标签的内容,用.string即可获取标签内部的文字
        3.Beautifulsoup
            表示的是一个文档的全部内容
        4.Comment
    5.遍历文档树
        1.直接子节点
                (1)
               tag 的 .content 属性可以将tag的子节点以列表的方式输出
               soup.head.contents[0]
                (2).children
                它返回的不是一个list(list生成器对象),不过我们可以遍历获取所以的子节点
        2.所有子孙节点
                .descendants 属性可以对所有tag的子孙节点进行递归循环
        3.节点内容
                 通过.string返回标签里面的内容 
        4.多个内容
                 .strings,获取多个内容,需要遍历获取
         5.父节点
                 p = soup.p
                 print p.parent.name
         6.全部父节点
                 content = soup.head.title.string
                 for parent in content.parents:
                     print(parent.name)
          7.兄弟节点
                .next_sibling 属性获取了该节点的下一个兄弟节点,.previous_sibling 属性获取了该节点的上一个兄弟节点,如果节点不存在,则返回 None
          8.全部兄弟节点
                .next_siblings 和 .previous_siblings 属性可以对当前节点的兄弟节点迭代输出  
          9.前后节点
                .previous_elements   .next_element

搜索文档树
        find_all(name , attrs , recursive , text , **kwargs )
//css选择器
        soup.select()                    

  一般简单网站(对反爬虫没有特别严格),基本上就能使用requests.get(url)直接获取信息,些微麻烦点的需要加上user_agent,等等。那么此处就对反爬的一些策略进行分析。

  世界上没有一个网站能做多完美的反爬虫,爬虫与反爬虫的战争就类似于攻与守。

  目前比较常规简单有效的手段就是采取后端的反爬虫。

  1.user_agent + referer检测

  2.账号及cookie验证

  3.验证码

  4.ip限制频次

  对于以上的防范措施,可以使用Chrome selenium或者phantomjs模拟浏览器环境,也就是常说的机器人、tessercat识别验证码、代理ip的替换

  以上方法对于一般的网站绰绰有余,反爬更加好一点的,参考下文:http://litten.me/2017/07/09/prevent-spiders/

 

posted @ 2019-10-08 11:27  是四不是十  阅读(387)  评论(0编辑  收藏  举报