欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python模拟登陆爬取接口返回数据插入数据库等

程序员文章站 2022-04-07 18:05:26
用python代码简单连接MySQL以及插入数据的两种方法https://www.cnblogs.com/zyde-2893/p/11267232.html此为连接数据库插入数据库操作python爬虫之模拟登录将cookie保存到代码中https://www.cnblogs.com/lxmtx/p/12636271.html#!/usr/local/bin/python3.7"""@File : cookiejar_login.py@Time : 2020/04/05...

用python代码简单连接MySQL以及插入数据的两种方法
https://www.cnblogs.com/zyde-2893/p/11267232.html

此为连接数据库插入数据库操作

python爬虫之模拟登录将cookie保存到代码中
https://www.cnblogs.com/lxmtx/p/12636271.html

#!/usr/local/bin/python3.7

"""
@File    :   cookiejar_login.py
@Time    :   2020/04/05
@Author  :   Mozili

"""

import urllib.request
import urllib.parse
# cookiejar用来保存cookie
import http.cookiejar

# 创建一个cookiejar对象
cj = http.cookiejar.CookieJar()
# 创建一个haddler对象
haddler = urllib.request.HTTPCookieProcessor(cj)
# 创建一个opener对象
opener = urllib.request.build_opener(haddler)

# post请求url
post_url = 'http://team.speedtest.cn/login'
# post请求参数
data = {
    'account':'mozili',
    'password':'xxx'
}
# 创建请求头部
headers = {
    'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15'
}
# 对参数进行处理
data = urllib.parse.urlencode(data).encode()
# 创建一个request
request = urllib.request.Request(url=post_url, headers=headers)
# 发送请求,注意使用opener
response = opener.open(request, data=data)
# 打印请求结果
print(response.read().decode())
print('------我是分界线-------')

# 登录成功后,进行get请求
get_url = 'http://team.speedtest.cn/?type=my'
request = urllib.request.Request(url=get_url, headers=headers)
response = opener.open(request)
print(response.read().decode())

此为使用python模拟登陆页面保存cookie再次访问页面

Python 获取接口数据,解析JSON,写入文件
https://www.cnblogs.com/0x03/p/7335305.html

import types
import urllib2
import json


duan ="--------------------------"	#在控制台断行区别的

#利用urllib2获取网络数据
def registerUrl():
	try:
		url ="http://m.weather.com.cn/data/101010100.html"
		data = urllib2.urlopen(url).read()
		return data
	except Exception,e:
		print e
		
#写入文件
def jsonFile(fileData):
	file = open("d:\json.txt","w")
	file.write(fileData)
	file.close()

#解析从网络上获取的JSON数据	
def praserJsonFile(jsonData):
	value = json.loads(jsonData)
	rootlist = value.keys()
	print rootlist
	print duan
	for rootkey in rootlist:
		print rootkey
	print duan
	subvalue = value[rootkey]
	print subvalue
	print duan
	for subkey in subvalue:
		print subkey,subvalue[subkey]
	
if __name__ == "__main__":
	# xinput = raw_input()
	# x = 130
	# xvalue = cmp(x,xinput)
	# print xvalue
	# print x/100.0
	
	data = registerUrl()
	# jsonFile(data)
	
	praserJsonFile(data)
	此为解析接口的json数据

其中遇到的问题
Python:requests:详解超时和重连
https://blog.csdn.net/weixin_33736649/article/details/91386897

pymysql.err.InterfaceError: (0, ‘’)解决办法
https://blog.csdn.net/okm6666/article/details/80618767


> db.ping(reconnect=True)
cur.execute(sql)
db.commit()

想获取个下标玩玩 Python的for循环显示每个元素的下标
https://blog.csdn.net/weixin_43873198/article/details/106895626

x = ['a', 'b', 'c']
for i, j in enumerate(x):
    print(i, j)

0 a
1 b
2 c

PHP中使用CURL模拟登录并获取数据实例
https://www.jb51.net/article/51707.htm

本文地址:https://blog.csdn.net/weixin_45237065/article/details/109643214

相关标签: python