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



  • 相关阅读:
    vscode 多文件编译
    Spring
    tomcat server.xml详细解析
    XML解析——Java中XML的四种解析方式
    MyBatis-config配置信息
    java学习笔记--JDBC实例
    50道经典的JAVA编程题(目录)
    Java8 函数式编程详解
    递归,--遍历多维数组
    eslint关闭配置--vue-webpack
  • 原文地址:https://www.cnblogs.com/jinjidedale/p/6040368.html
Copyright © 2011-2022 走看看