7160测试

#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))

 

posted @ 2018-09-19 22:33  brady-wang  阅读(641)  评论(0编辑  收藏  举报