示例网站:
http://exercise.kingname.info/exercise_requests_get.html
GET 测试 (kingname.info)http://exercise.kingname.info/exercise_requests_get.html
import requests html = requests.get('http://exercise.kingname.info/exercise_requests_get.html').content.decode() print(html)
post方法的简单使用;
使用get方法访问post网页时的报错
import requests html = requests.get('http://exercise.kingname.info/exercise_requests_post').content.decode() print(html)
改用post方法来对网页进行访问
mport requests import re data = {'name':'liyong','password':'li123'} html = requests.post('http://exercise.kingname.info/exercise_requests_post',data=data).content.decode() html_data = re.finall() print(html)
改为json格式提交数据
import requests import re data = {'name':'liyong','password':'li123'} html = requests.post('http://exercise.kingname.info/exercise_requests_post',json=data).content.decode() #html_data = re.finall() print(html)
使用正则表达式提取数据
import requests import re data = {'name':'liyong','password':'li123'} html = requests.post('http://exercise.kingname.info/exercise_requests_post',json=data).content.decode() html_title = re.findall('title>(.*?)<',html,re.S) html_data = re.findall('p>(.*?)<',html,re.S) content = '\n'.join(html_data) print("title is :",html_title) print("data is :",content) #print(html)