• 网络爬虫基础练习


    #爬取内容选自一篇搜狐文章
    import
    requests from bs4 import BeautifulSoup res = requests.get('http://www.sohu.com/a/226664758_153839') res.encoding = 'UTF-8' soup = BeautifulSoup(res.text, 'html.parser')

    取出h1标签的文本:

    for h1 in soup.find_all('h1'):
        print(h1.text)

    取出a标签的链接:

    for a in soup.find_all('a'):
        print(a.attrs.get('href'))

    取出所有li标签的所有内容:

    for li in soup.find_all('li'):
         print(li.contents)

    取出一条新闻的标题、链接、发布时间、来源:

    print(soup.select('div.article-info')[0].text)
    print(soup.select('div .text-title')[0].find('h1').text)
  • 相关阅读:
    WAMP Apache 2.5 配置虚拟主机
    DOM对象
    BOM对象
    JS内置对象
    CSS定位
    CSS浮动和清除
    浏览器兼容性
    垂直居中
    水平居中总结
    长度值
  • 原文地址:https://www.cnblogs.com/224yang/p/8670767.html
Copyright © 2020-2023  润新知