9--爬虫(1)

Python 如何访问互联网
在这里插入图片描述在这里插入图片描述

urllib
>>> import urllib.request
>>> response = urllib.request.urlopen("http://www.fishc.com")
>>> html=response.read()
>>> print(html)

在这里插入图片描述

>>> html=html.decode("utf-8")  //修改编码
>>> print(html)

在这里插入图片描述

示例一:一个翻译器

import urllib.request
import urllib.parse
import json
content=input("请输入要翻译的内容:")
url="http://fanyi.youdao.com/translate?smartresult=dict,rule"
data={}
data["i"]=content
data["from"]="AUTO"
data["to"]="AUTO"
data["smartresult"]="dict"
data["client"]="fanyideskweb"
data["salt"]="15941988694158"
data["sign"]="a2e5ff147cfdd8f3873cba9bc6e2824e"
data["ts"]="1594198869415"
data["bv"]="e2a78ed30c66e16a857c5b6486a1d326"
data["doctype"]="json"
data["version"]="2.1"
data["keyfrom"]="fanyi.web"
data["action"]="FY_BY_CLICKBUTTION"
data=urllib.parse.urlencode(data).encode('utf-8')

response=urllib.request.urlopen(url,data)
html=response.read().decode('utf-8')

target=json.loads(html)
print("翻译结果:%s" % (target['translateResult'][0][0]['tgt']))

在这里插入图片描述示例二:下载一张指定大小的图片

import urllib.request

response=urllib.request.urlopen('http://placekitten.com/g/500/600')
cat_img = response.read()

with open("cat_500_600.jpg",'wb') as f:
	f.write(cat_img)

猜你喜欢

转载自blog.csdn.net/qq_44108455/article/details/107211334
今日推荐