Import Urllib, urllib.request, urllib.parseimport randomimport zlibimport reimport os, timesavepath= "Save" Def InitDev () : Allhanzi = "" If not Os.path.exists (Savepath): Os.makedirs (Savepath) with open ("All.txt", mode= "R", Enco ding= "Utf-8") as F:allhanzi=f.read () return allhanzidef savegif (zi): url = r "https://xxxxxxxxx.xxxxxxxxxx.co M/s? {} "data={" wd ": Zi," from ":" XXXX "} datastr=urllib.parse.urlencode (data) URL = Url.format (datastr) US Er_agents = ["mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.1 (khtml, like Gecko) chrome/22.0.1207.1 safari/537.1 "," mozilla/5.0 (X11; CrOS i686 2268.111.0) applewebkit/536.11 (khtml, like Gecko) chrome/20.0.1132.57 safari/536.11 "," mozilla/5.0 (Wind OWS NT 6.1; WOW64) applewebkit/536.6 (khtml, like Gecko) chrome/20.0.1092.0 safari/536.6 "," mozilla/5.0 (Windows NT 6.2) Applew ebkit/536.6 (khtml, like Gecko) chrome/20.0.1090.0 safari/536.6 "," mozilla/5.0 (Windows NT 6.2; WOW64) applewebkit/537.1 (khtml, like Gecko) chrome/19.77.34.5 safari/537.1 "," mozilla/5.0 (X11; Linux x86_64) applewebkit/536.5 (khtml, like Gecko) chrome/19.0.1084.9 safari/536.5 "," mozilla/5.0 (Windows NT 6.0) applewebkit/536.5 (khtml, like Gecko) chrome/19.0.1084.36 safari/536.5 "," mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/536.3 (khtml, like Gecko) chrome/19.0.1063.0 safari/536.3 "," mozilla/5.0 (Windows NT 5.1) Applew ebkit/536.3 (khtml, like Gecko) chrome/19.0.1063.0 safari/536.3 "," mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) applewebkit/536.3 (khtml, like Gecko) chrome/19.0.1063.0 safari/536.3 "," mozilla/5.0 (window S NT 6.2) applewebkit/536.3 (khtml, like Gecko) chrome/19.0.1062.0 safari/536.3 "," mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/536.3 (khtml, like Gecko) chrome/19.0.1062.0 safari/536.3 "," mozilla/5.0 (Windows NT 6.2) Applew ebkit/536.3 (khtml, like Gecko) chrome/19.0.1061.1 safari/536.3 "," Mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/536.3 (khtml, like Gecko) chrome/19.0.1061.1 safari/536.3 "," mozilla/5.0 (Windows NT 6.1) Applew ebkit/536.3 (khtml, like Gecko) chrome/19.0.1061.1 safari/536.3 "," mozilla/5.0 (Windows NT 6.2) applewebkit/536.3 ( Khtml, like Gecko) chrome/19.0.1061.0 safari/536.3 "," mozilla/5.0 (X11; Linux x86_64) applewebkit/535.24 (khtml, like Gecko) chrome/19.0.1055.1 safari/535.24 "," mozilla/5.0 (Windows NT 6. 2; WOW64) applewebkit/535.24 (khtml, like Gecko) chrome/19.0.1055.1 safari/535.24 "," mozilla/5.0 (Windows; U Windows NT 5.1; En-US) applewebkit/531.21.8 (khtml, like Gecko) version/4.0.4 safari/531.21.10 "," mozilla/5.0 (Windows; U Windows NT 5.2; En-US) applewebkit/533.17.8 (khtml, like Gecko) version/5.0.1 safari/533.17.8 "," mozilla/5.0 (Windows; U Windows NT 6.1; En-US) applewebkit/533.19.4 (khtml, like Gecko) version/5.0.2 safari/533.18.5 "," mozilla/5.0 (Windows; U Windows NT 6.1; EN-GB;rv:1.9.1.17) gecko/20110123 (like firefox/3.x) seamonkey/2.0.12 "," mozilla/5.0 (Windows NT 5.2; rv:10.0.1) gecko/20100101 firefox/10.0.1 seamonkey/2.7.1 "," mozilla/5.0 (Macintosh; U Intel Mac OS X 10_5_8; En-US) applewebkit/532.8 (khtml, like Gecko) chrome/4.0.302.2 safari/532.8 "," mozilla/5.0 (Macintosh; U Intel Mac OS X 10_6_4; En-US) applewebkit/534.3 (khtml, like Gecko) chrome/6.0.464.0 safari/534.3 "," mozilla/5.0 (Macintosh; U Intel Mac OS X 10_6_5; En-US) applewebkit/534.13 (khtml, like Gecko) chrome/9.0.597.15 safari/534.13 "," mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) applewebkit/535.1 (khtml, like Gecko) chrome/14.0.835.186 safari/535.1 "," mozilla/5.0 (macin Tosh Intel Mac OS X 10_6_8) applewebkit/535.2 (khtml, like Gecko) chrome/15.0.874.54 safari/535.2 "," mozilla/5.0 (macint Osh Intel Mac OS X 10_6_8) applewebkit/535.7 (khtml, like Gecko) chrome/16.0.912.36 safari/535.7 "," mozilla/5.0 (macint Osh U Mac OS X Mach-O; En-us; rv:2.0a) gecko/20040614 firefox/3.0.0 "," mozilla/5.0 (Macintosh; U PPC Mac OS X 10.5; En-us; rv:1.9.0.3) gecko/2008092414 firefox/3.0.3 "," mozilla/5.0 (Macintosh; U Intel Mac OS X 10.5; En-us; rv:1.9.1) gecko/20090624 firefox/3.5 "," mozilla/5.0 (Macintosh; U Intel Mac OS X 10.6; En-us; rv:1.9.2.14) gecko/20110218 alexatoolbar/alxf-2.0 firefox/3.6.14 "," mozilla/5.0 (Macintosh; U PPC Mac OS X 10.5; En-us; rv:1.9.2.15) gecko/20110303 firefox/3.6.15 "," mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) gecko/20100101 firefox/4.0.1 "] headerstr = {" User-agent ": Random.choice (user_agents)," H OST ":" Xxxxx.baidu.com "," Accept ":" Text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image /apng,*/*;q=0.8 "," accept-encoding ":" gzip, deflate, BR "," Accept-language ":" Zh-cn,zh;q=0 .9 ", #" Cookie ":" pstm=1527352228; bidupsid=50ba11b43cd76a00613eb02151d5e961; baiduid=eb49a1306b852804b150538899b2632a:fg=1; bdorz=b490b5ebf6f3cd402e515d22bcda1598; h_ps_pssid=1453_21101; hm_lvt_010e9ef9290225e88b64ebf20166c8c4=1527941967,1527994003; Psino=1; hm_lpvt_010e9ef9290225e88b64ebf20166c8c4=1528002586 "," Referer ":" Https://xxxxx.xxxxx.xxxx "} Try: Req=urllib.request.request (Url=url, Headers=headerstr, method= "GET") Response=urllib.request.urlopen (req) html = response.read () Decompressed_data = zlib.decompress (HTML, + zlib. max_wbits) Text = Decompressed_data.decode (' UTF8 ') List = Re.findall (r "Data-gif=\" (. *?) \ "", text) print (list) DestFile = "{}\\{}.gif". Format (Savepath, Zi) if (list.__len__ () > 0): Urllib.request.urlretrieve (List[0], destfile) saveresult (Zi, True) else:saveresult (zi , false) except Exception as E:saveresult (Zi, False) Time.sleep (1) def saveresult (str, success): Filestr = "Error.log" IF success:filestr = "Ok.log" with open (Filestr, "A +") as F:f.write (str) def saveall (): Allhanzi = Ini Tdev () for x in range (len (Allhanzi)): Print (allhanzi[x], "\ r \ n") savegif (Allhanzi[x]) if __name__ = = ' __ma In__ ': SaveAll ()
Python3 visit Baidu return compression format