数学建模社区-数学中国

标题: Python-搜狗post上传图片 [打印本页]

作者: 檀俾九    时间: 2021-1-11 08:42
标题: Python-搜狗post上传图片

, G  y0 V4 T) k0 Q3 q: I[code=python]#!/usr/bin/python
- R7 m5 `' O. R$ H# -*- coding: utf-8 -*-/ y8 ^; G$ u4 k! [+ @; b
2 f* [. x; b: M6 N# Y/ W
import tkinter as tk: s+ @; d, v6 s! i4 h
from tkinter import filedialog
6 ^$ C: Z! p+ d) o/ F  h
8 V+ `& Z" B+ ~  r0 E* f& Y2 \import requests$ }/ q2 K+ _' c( n. T( R
from urllib3 import encode_multipart_formdata
" ^3 c3 }' u/ A2 s& b) w+ N$ {2 r& T0 O
from os.path import basename1 K8 h! n; U  t" W
% t$ i# W- v+ k' o
from PIL import Image
$ W5 w( f) l! i+ C, F0 V( ?5 {# d- o* ], D: h8 u1 C
0 d/ h" Z7 _8 x& U# S5 G# \
def getmidstring(html, start_str, end):0 B! b, H0 ~; P% o' d- c, n
    start = html.find(start_str)
& r+ W" q" b  y/ _. G& \, V    if start >= 0:. ?9 w" l- z1 M4 f
        start += len(start_str)! ^: `4 M2 C7 z9 c
        end = html.find(end, start)$ Y; x/ h0 }; e, y( y- \: r
        if end >= 0:
' d1 Z. k& R! |* K7 h            return html[start:end].strip()$ Y' p' S4 [( h

* p: a& i, t0 c( L' ~" U* [3 q3 y: G/ p! W" J3 h
def getTextLeft(html, end):( ~% ^1 W) Q8 v/ }( d3 a' u1 t% E
    start = html.find(end)3 O  o6 F. v8 R& W* m$ _7 `, Y
    if start >= 0:# b' Y) D* s9 E  o0 }- c
        return html[0:start].strip()
8 ?- K- E5 g$ i" u+ r4 `& G, o, L- `. j8 _' d

! h, L. h) c! y- ^. Q. kroot = tk.Tk(). B/ V: f8 y; ]; X& X
root.withdraw()0 r- P& i! {' c6 Y0 j- C( b

' |# i8 t) L# R0 N; y6 L  sfiletypes = [(
. U4 U2 W3 I' U9 Q5 E, X9 `    '图片文件(*.jpg;*.jpeg;*.gif;*.png;*.bmp)', '*.jpg;*.jpeg;*.gif;*.png;*.bmp')]
- k3 Z7 V7 ?" y6 V7 W" H# ofile_paths = filedialog.askopenfilenames(filetypes=filetypes)
9 r. |* ~3 z7 I! O& d/ c9 ?; G, b' Y: k
for file_path in file_paths:3 z4 z8 s" o. v( [! X. |" `
    print(file_path)  X& d- P7 P7 P" ]# \0 s
$ J$ [7 h0 M" B/ h- K
    im = Image.open(file_path)
' z2 L; `! b$ c    print("image/" + im.format.lower())
3 M5 o  r1 U1 ]& F7 G, t" m/ s& e6 D+ b* x
    with open(file_path, mode="rb") as f:# V  E5 K& {! ~. [
        file = {
# R+ J# `8 G- F# J            "flag": "1",5 Q4 F) X$ B( |2 @( l, i
            "pic_path": (basename(file_path), f.read()),
# L* D) _; t) \        }
$ w$ Q4 g& w  v8 |# z9 B3 |- ]        encode_data = encode_multipart_formdata(file)
+ w# M8 }0 z: B0 t/ L& n1 F; d, M
        file_data = encode_data[0]
2 ?) e$ v) E' m0 q6 \2 Y+ ~# G2 |
        print(encode_data[1])
' z. m% n& ~! B& u% T; S( ^% C# x6 e& H0 C; @
    url = "http://pic.sogou.com/ris_upload"
; u3 ~. ?  f# Y+ Y- l0 y. m* C+ k% ~% E    headers_from_data = {
- B9 m6 n5 F; [0 A        "Content-Type": encode_data[1]0 H3 U. v: C" v# f3 A+ J
    }
* h8 w* z( ]" I3 d    response = requests.post(6 M; B/ C$ j% F% H) t' z- [9 a
        url=url,  q  Q3 f7 T2 w' J" \# ?- y
        headers=headers_from_data," l* o8 w6 w2 ?& j2 m
        data=file_data,# x) b. _* q: f6 q) }" O
        allow_redirects=False)2 x2 e$ B# M! g8 D; i$ a: T9 E
    response_text = response.text; n  O9 u, Z2 m$ X$ r
    start = response_text.find("<a href=\"")
% R0 b5 F) D$ g& g0 ?  k    start = getmidstring(response_text, "<a href=\"", "\">")
9 N" f1 J+ E2 @9 C    print("相似图片:" + start)
; ]5 C& y$ j- V
- ?( Z0 K, `2 j) Q8 ^, h: V    start = getTextLeft(start, "&oname=")/ G8 c. J3 i& |! h" E. |5 b
    start = start + "&flag=1&reqType=ajax&st=12&reqFrom=result"
  `. z9 v. v# y5 @% A& P+ r5 D. Q
% z4 l. N- \7 T# D+ U    start = requests.get(start).json(). n$ D$ J, Z" X3 P  V. r
    print("上传的图片:" + start['query'])6 R; c" u8 f/ k2 [* s  }+ g" y
[/code]
9 [! }% q0 h, g* a2 L
' T. D4 l" Q* l/ G% V. [8 V转发自派生社区( _  ~( C) j9 x& b





欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5