python抓取页面中主要信息的方法(二)

天下的网站没有我爬不到的,只有不想爬的(有吹牛逼之嫌)。

Python2慢慢被Python3所代替了,主要以3为主,话不多说,直接看技术点吧

  1. 正则表达式re(难)
    1. 获取<tr></tr>标签之间内容
    2. 获取<a href..></a>超链接之间内容
    3. 获取URL最后一个参数命名图片或传递参数
    4. 爬取网页中所有URL链接
    5. 爬取网页标题title两种方法
    6. 定位table位置并爬取属性-属性值
    7. 过滤<span></span>等标签
    8. 获取<script></script>等标签内容
    9. 通过replace函数过滤<br />标签
    10. 获取<img ../>中超链接及过滤<img>标签
    • 代码:
    • import re
      content = 
      <td> 
      <a href="https://www.baidu.com/articles/zj.html" title="山西省">山西煤多</a> 
      <a href="https://www.baidu.com//articles/gz.html" title="北京市">北京人多</a> 
      </td> 
      # 获取<a href></a>之间的内容
      print(u'获取链接文本内容:')
      res = r'<a .*?>(.*?)</a>'
      mm = re.findall(
          res, content, re.S | re.M)
      for value in mm:
          print(value)
      # 获取所有<a href></a>链接所有内容
      print(u'\n获取完整链接内容:')
      urls = re.findall(r"<a.*?href=.*?<\/a>", content, re.I | re.S | re.M)
      for i in urls:
          print(i)
      # 获取<a href></a>中的URL
      print(u'\n获取链接中URL:')
      res_url = r"(?<=href=\").+?(?=\")|(?<=href=\').+?(?=\')"
      link = re.findall(res_url, content, re.I | re.S | re.M)
      for url in link:
          print(url)
      
      
    • 结果:
    • 获取链接文本内容:
      山西人多
      北京人多
      获取完整链接内容:
      < a
      href = "https://www.baidu.com/articles/zj.html"
      title = "山西省" > 山西人多 < / a >
      < a
      href = "https://www.baidu.com//articles/gz.html"
      title = "北京市" > 北京人多 < / a >
      获取链接中URL:
      https: // www.baidu.com / articles / zj.html
      https: // www.baidu.com // articles / gz.html  
  2. 基于bs4的BeautifulSoup模块(中)
    • import urllib.request,
      lxml.html
      
      url = 'http://www.nmc.cn'
      html = urllib.request.urlopen(url).read()
      tree = lxml.html.fromstring(html)
      content = tree.cssselect('li.waring > a')
      for n in content:
          link = n.get('href')
          title = n.get('title')
          tag = n.text
          print(tag, url + link, title)
  3. 基于lxml的etree模块(中)
    • from bs4 import BeautifulSoup
      import urllib.request
      
      url = 'http://www.nmc.cn'
      html = urllib.request.urlopen(url).read()
      soup = BeautifulSoup(html,'lxml')
      content = soup.select('#alarmtip > ul > li.waring > a')
      ######### 添加到列表中
      link = []
      title = []
      tag = []
      for n in content:
          link.append(url+n.get('href'))
          title.append(n.get('title'))
          tag.append(n.text)
      ######## 添加到字典中
      for n in content:
          data = {
              'tag'   : n.text,
              'link'  : url+n.get('href'),
              'title' : n.get('title')
          }

我没有习惯用的方法,有别的方法,请大家留言

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

依剑仗天涯

你的鼓励是我创装的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值