current position:Home>Application of urllib package in Python

Application of urllib package in Python

2022-01-31 11:26:03 Invincible white

This is my participation 11 The fourth of the yuegengwen challenge 11 God , Check out the activity details :2021 One last more challenge

python Medium urllib Package is mainly used to operate web pages , It mainly includes :

  • urllib.request - Open and read URL.
  • urllib.error - contain urllib.request Exception thrown .
  • urllib.parse - analysis URL.
  • urllib.robotparser - analysis robots.txt file .

stay urllib The methods of , The more common method is request, It defines URL function , Including browser opening 、cookies Storage 、 Redirection and so on .

Read the contents of the form and send web request

When we need to open and log in to the specified website , You can use urllib2.Request(url_login,data1) Request method of , Among them data Data can be read from tables .

Yes excel Description of the form :excel Data in ( Numbers ) Must be in text format , To see the green triangle in the upper left corner , It's best not to copy and paste in , Prone to format errors , It's better to enter it directly .

Here, after reading the parameters in the table , Convert to json Format , And pass request Method request out .

import xlrd
import json

import urllib,urllib2

import string

data=xlrd.open_workbook("test.xls")

table=data.sheet_by_name("data")

nrows=table.nrows

ncols=table.ncols

returnData={}

for i in range(nrows):

returnData[table.cell(i,0).value]=table.cell(i,1).value

returnJson=json.dumps(returnData)

returnJson=json.loads(returnJson)

print returnJson

url_login='http://192.000.000.000/jdstore-console/user/checklogin'

# data1={"mobile":"18200000000","password":"000000","roleId":"R001"}

# print data1

data1=urllib.urlencode(returnJson)

req=urllib2.Request(url_login,data1)

res=urllib2.urlopen(req)

res=res.read()

print res
 Copy code 

There are return parameters It indicates that the interface has been connected , The rest is about parameters ( Content 、 Format json) If nothing returns , The interface is not connected , It may be a server problem ,header problem (cookie) wait .

post Parameter submission format

data={'isEnable':'false','name':' How have you been? ','questions':[{"isDelete":"false","isRequire":"false","type":0,"name":" The first question is ","orders":"null","id":"null","defaultValue":"{"0":"1","1":"2","2":"3"}","isNew":"true"}],'remark':' remarks ','users':'8d43fa4ce3e9434fab6cac7ccdb3d0fd'}

There is no need for single quotation marks for the parts in brackets .

post send out data Another data format for data : urlencoded

for example :POST www.example.com HTTP/1.1 Content-Type: application/x-www-form-urlencoded;charset=utf-8 title=test&sub%5B%5D=1&sub%5B%5D=2&sub%5B%5D=3

In the next issue, we will continue to talk about cookie Request method of .

copyright notice
author[Invincible white],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201311126012697.html

Random recommended