编程语言
首页 > 编程语言> > python获取网页数据

python获取网页数据

作者:互联网

第一:下载图片

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)

第二:使用有道翻译,加请求头,加访问data

import urllib.request
import urllib.parse
import json


content = input('输入需要翻译的内容:')

url = 'https://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
data = {}
data['i']= content
data['from']='AUTO'
data['to']='AUTO'
data['smartresult']='dict'
data['client']='fanyideskweb'
data['salt']='16226799592359'
data['sign']='36b4d3a6f8cec877cb56f24fb64f85bb'
data['lts']='1622679959235'
data['bv']='9ff8102373b1562471f4b6881a5653e9'
data['doctype']='json'
data['version']= '2.1'
data['keyfrom']='fanyi.web'
data['action']='FY_BY_REALTlME'
data = urllib.parse.urlencode(data).encode('UTF-8')

#添加请求头
head = {}
head['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'


req= urllib.request.Request(url,data,head)
response = urllib.request.urlopen(req)
html = response.read().decode('UTF-8')

target = json.loads(html)

res = target['translateResult'][0][0]['tgt']

print('翻译结果:%s' %(res))

第三:如何使用代理

import urllib.request

url = 'http://www.whatismyip.com.tw/'

proxy_support = urllib.request.ProxyHandler({'http':'119.6.144.73:81'})
opener = urllib.request.build_opener(proxy_support)
urllib.request.install_opener(opener)

response = urllib.request.urlopen(url)
html = response.read().decode('UTF-8')
print(html)

 

标签:网页,python,request,urllib,获取,opener,import,data,response
来源: https://blog.csdn.net/weixin_46054799/article/details/117509955