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等方法获取各种属性



  • 相关阅读:
    STS 配置tomcat以后,无法访问
    docker
    Java
    STS
    Java
    docker
    sql产生随机时间
    sql产生随机数
    Android 代码自动提示功能
    Activity的跳转与传值
  • 原文地址:https://www.cnblogs.com/jinjidedale/p/6040368.html
Copyright © 2011-2022 走看看