7160测试

时间:2018-09-19 23:08:00   收藏:0   阅读:242
#coding=utf-8
import requests
from lxml import etree
import urllib
url = ‘http://www.7160.com/zhenrenxiu/56786/‘

def parse_content(content):
    tree = etree.HTML(content)
    title = tree.xpath("/html/body/div/div[2]/div[1]/div[2]/h1/text()")
    url = tree.xpath("/html/body/div/div[2]/div[1]/div[2]/div[3]/p/a/img/@src")
    print(title)
    print(url[0])
    return (title[0],url[0])


def store_img(title,img_url):

    urllib.request.urlretrieve(img_url, ‘/craw/‘+title+".jpg")
    print(‘/craw/‘ + title + ".jpg 保存成功")
try:
    res = requests.get(url)
    res.encoding = ‘gb2312‘

    #解析 返回url
    title,img_url = parse_content(res.text)

    store_img(title,img_url)

except Exception as e:
    print("异常"+str(e))

 

原文:https://www.cnblogs.com/php-linux/p/9678099.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!