zoukankan      html  css  js  c++  java
  • python网络数据采集之beautifulsoup

    beautifulsoup中常用的方法findall与find,清楚这俩个方法的关系和用法
    其中还有
    .children标签

    from urllib.request import urlopen
    from bs4 import BeautifulSoup
    html = urlopen("http://www.pythonscraping.com/pages/page3.html")
    bsObj = BeautifulSoup(html)
    for child in bsObj.find("table",{"id":"giftList"}).children:
    print(child)

    兄弟标签next_siblings()

    from urllib.request import urlopen
    from bs4 import BeautifulSoup
    html = urlopen("http://www.pythonscraping.com/pages/page3.html")
    bsObj = BeautifulSoup(html)
    for sibling in bsObj.find("table",{"id":"giftList"}).tr.next_siblings:
    print(sibling)



    这里通过上述的方法找到div class=pl2下的 a标签下的title

    # coding=utf-8
    from urllib2 import urlopen
    from bs4 import BeautifulSoup
    import re

    html = urlopen("https://book.douban.com/top250?start=0")
    bsObj = BeautifulSoup(html)

    for link in bsObj.findAll("div",attrs={"class":"pl2"}):
    name=link.find("a")
    print name.get('title')

    如果改成
    for link in bsObj.findAll("div",attrs={"class":"pl2"}):
    name=link.findAll("a")
    print name[0].get('title')
    效果是一样的

    还能通过name.text获取a标签中的文本内容
    .get('href')
    .val等方法获取各种属性



  • 相关阅读:
    delphi XE8 for android ----一个无意闯入的世界
    不能Ping和telnet的
    syslog-ng内容讲解
    《信息安全系统设计与实现》学习笔记7
    缓冲区溢出实验
    2.3.1测试
    鲲鹏服务器测试
    cat userlist
    需求分析
    《信息安全系统设计与实现》学习笔记5
  • 原文地址:https://www.cnblogs.com/jinjidedale/p/6040368.html
Copyright © 2011-2022 走看看