爬虫基础库

作者: 蜡笔小姜和畅畅 | 来源:发表于2018-07-11 09:23 被阅读19次
Screen Shot 2018-07-11 at 9.22.53 AM.png

概要

介绍下requests和BeautifulSoup两个库的基本使用

具体内容

  • requests
    requests是一个模拟浏览器发送请求的库
    • methods
      具体的http请求类型:
      GET对应 requests.get()
      POST对应 requests.post()
    • url
      对应的http请求地址
      url = 'http://www.cnblogs.com/wupeiqi/p/9078770.html'
      requests.get(url=url)
    • header
      http请求的请求头
      header = {'Content-Type': 'image/jpeg'}
      requests.get(url=url, header=header)
    • cookie
      http请求的缓存
      cookie = {'_gid': 'GA1.2.1083957064.1531274683'}
      requests.get(url=url, cookie=cookie)
    • 上传文件
      file = {''file'': open('report.xls', 'rb')}
      requests.get(url=url, file=file)
  • BeautifulSoup
    BeautifulSoup是一个可以从HTML或XML文件中提取数据的Python库
    • 初始化
      soup = BeautifulSoup(请求返回的html文本,'html.parser')
    • find
      找到上一篇、下一篇的div标签
      soup.find(name = 'div', id = 'post_next_prev')
    • find_all
      查询所有的a标签soup.find_all('a')
    • get
      获取div标签里面的链接
      soup.get('href')
      图片链接
      soup.get('src')

相关文档

相关文章

网友评论

    本文标题:爬虫基础库

    本文链接:https://www.haomeiwen.com/subject/bubcpftx.html