美文网首页
xpath的练习与模仿1

xpath的练习与模仿1

作者: 名字就起八个字吧 | 来源:发表于2018-07-13 19:21 被阅读0次

# -*- coding: utf-8 -*-

"""

Created on Sat Apr 14 10:28:35 2018

@author: Administrator

"""

global pic_link

#pic_link=[]

import requests

#import random

from lxml import etree

def load_image(url):

    headers={"User-Agent":"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0"}

    html=requests.get(url,headers=headers)

    content=etree.HTML(html.text)

    link=content.xpath('//*[@id="bigpicpic"]/@src',stream=True)

    return str(link[0])

def save_image(pic_link,i):

    with open('D:/python/python_work/CSDN/pic_{}.gif'.format(i),'wb') as f:

        picture=requests.get(pic_link).content

        f.write(picture)

if __name__=='__main__':

    for i in range (1, 21):

        url='http://photo.hupu.com/p10087-{}.html'.format(i)

        tail=load_image(url)

        pic_link='http:'+tail

        save_image(pic_link,i)


最近在学XPATH,随便模仿写了段爬虎扑搞笑图片的代码。

相关文章

网友评论

      本文标题:xpath的练习与模仿1

      本文链接:https://www.haomeiwen.com/subject/gwldkftx.html