python12

import re
import urllib.request

#urllib.request.urlcleanup()

超时设置

for i in range(0,5):
file=urllib.request.urlopen(“https://read.douban.com/provider/all”,timeout=1)
try:
print(len(file.read().decode(“utf-8”))) #值太大了,可以输出长度
except Exception as err:
print(“出现异常!”)

#自动模拟http请求 --向网页发送http请求(post,get)

get请求–实现百度信息自动搜索

keywd = “Python”
keywd = urllib.request.quote(keywd) #转码
url = “http://www.baidu.com/s?wd=” + keywd #注意:http 千万不要加s
data = urllib.request.urlopen(url).read().decode(“utf-8”) # get 直接爬
pat = ‘’‘title:’(.?)’,|title":"(.?)",’’’ #写法1
result = re.compile(pat).findall(data)
print(result)
‘’’
成功了
‘’’

keywd = “韦玮”
keywd = urllib.request.quote(keywd) #转码

page = (num-1)*10

for i in range(1,11):
url = “http://www.baidu.com/s?wd=” + keywd + “&pn=” + str((i-1)10) # 注意:http 千万不要加s
data = urllib.request.urlopen(url).read().decode(“utf-8”) # get 直接爬
pat1 = "title:’(.
?)’," # 写法2
pat2 = ‘title":"(.*?)",’
result1 = re.compile(pat1).findall(data)
result2 = re.compile(pat2).findall(data)

for j in range(0,len(result1)):
    print(result1[j])
for z in range(0,len(result2)):
    print(result2[z])
print(result1)
print(result2)

‘’’
韦玮_百度百科
韦玮_百度图片
韦玮_歌曲在线试听_网易云音乐
韦玮(女演员)_百度百科

‘’’

post 请求

‘’’
https://www.iqianyue.com/mypost/
‘’’

import urllib.parse

posturl = “https://www.iqianyue.com/mypost/”
postdata = urllib.parse.urlencode(
{
“name”:“111”,
“pass”:“222”,
}
).encode(“utf-8”)
#进行post,就需要使用urllib.request下面的Request(真实post地址,post数据)
req = urllib.request.Request(posturl,postdata)
result = urllib.request.urlopen(req).read().decode(“utf-8”)
print(result)
‘’’

Post Test Page name:
passwd:

you input name is:111
you input passwd is:222 '''

posturl = “https://www.iqianyue.com/mypost/”
postdata = urllib.parse.urlencode( # 构造数据 注意:数据是以字典形式构造的
{
“name”:“111”,
“pass”:“222”,
}
).encode(“utf-8”)
#进行post,就需要使用urllib.request下面的Request(真实post地址,post数据)
req = urllib.request.Request(posturl,postdata) # 直接先请求一下
result = urllib.request.urlopen(req).read().decode(“utf-8”) # 把请求爬下来
fh = open(“D:\百度网盘\post.html”,“w”) # 存在本地
fh.write(result)
fh.close()
‘’’
成功了
‘’’

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值