zoukankan      html  css  js  c++  java
  • Python爬虫之BeautifulSoup和requests的使用

    requests,Python HTTP 请求库,相当于 Android 的 Retrofit,它的功能包括 Keep-Alive 和连接池、Cookie 持久化、内容自动解压、HTTP 代理、SSL 认证、连接超时、Session 等很多特性,同时兼容 Python2 和 Python3。

    第三方库的安装:

    pip install urllib

    pip install requests

    小爬虫代码如下:

    # -* - coding: UTF-8 -* -
    #导入第三方库
    import urllib
    from bs4 import BeautifulSoup
    import requests
    url='https://www.phb123.com/junshi/lishi/9679_2.html'
    local="E:\py\imgs\" #保存图片的文件夹
    html_doc=requests.get(url).text
    soup=BeautifulSoup(html_doc,'lxml') #解析 html_doc
    contens=soup.find_all('center')
    x=1
    for con in contens:
    imgs=con.find_all('img') #获取center标签下的img标签
    for img in imgs:
    urllib.request.urlretrieve(img['src'], local + '%s.jpg' % (x))
    x =x+1



  • 相关阅读:
    Roadblocks(poj 3255)
    最小集合(51nod 1616)
    绿色通道(codevs 3342)
    解的个数(codevs 1213)
    多米诺(codevs 3052)
    abcd
    dwarf tower
    第K 小数
    noip2016复习
    文化之旅(洛谷 1078)
  • 原文地址:https://www.cnblogs.com/ling-yu/p/9182277.html
Copyright © 2011-2022 走看看