mysql怎么注入爬虫 mysql手工注入( 二 )


url = ''
pwd = '666666'
cousor.execute(sql_2,[pwd,url])
1
2
3
1
2
3
4.执行查询数据的sql语句
result1 = cousor.fetchone()
fetchone() 查询=整个表中的第一条数据,
如果再次使用就会查找到第二条数据,
还可以在括号内输入id值查询到相应的数据
result2 = cousor.fetchmany()
fetchmany()查询到表里的多条数据,
在括号里输入几就会查找到表的前几条数据
result2 = cousor.fetchall()
fetchall()查询到sql查询匹配到的所有数据
print(result)
用print输出语句就能直接打印输出所查询到的数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
**总结: 在执行sql语句要传入参数时,这个参数要以列表或者元组的类型传入**
1
1
七.关闭光标对象
cousor.close()
1
1
八.关闭数据库的链接对象
coon.cousor()
1
1
九.洛克王国宠物数据抓取案例
import requests
import pymysql
from lxml import etree
from time import sleep
# 数据库链接
conn = pymysql.connect(host='127.0.0.1', user='root', password='123456', database='pymysql')
cursor = conn.cursor()
# 执行一条创建表的操作
cursor.execute(
'''create table if not exists pets(id int primary keyauto_increment,name varchar(50),src varchar(100),industry text)''')
url = ''
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36'
}
response = requests.get(url=url, headers=headers)
response.encoding = 'gbk'
html = response.text
# print(html)
# 宠物名称
# 宠物图片(图片在 lz_src)
# 宠物技能(跳转详细页)
tree = etree.HTML(html)
li_list = tree.xpath('//ul[@id="cwdz_list"]/li')# 所有的宠物
for li in li_list:
name = li.xpath('./@name')[0]# 每一个宠物的名称
src = 'https://www.04ip.com/post/http:' + li.xpath('./a/img/@lz_src')[0]# 图片链接
link = '' + li.xpath('./a/@href')[0]# 宠物的详细链接
industry = []# 数组里面存放每一个对象,每一个对象就是一个技能
# 对详细链接发起请求,获取技能
try:
detail_resp = requests.get(url=link, headers=headers)
sleep(0.5)
detail_resp.encoding = 'gbk'
detail_tree = etree.HTML(detail_resp.text)
# 技能
skills = detail_tree.xpath('/html/body/div[5]/div[2]/div[2]/div[1]/div[1]/table[4]/tbody/tr')
del skills[0]
del skills[0]
for skill in skills:
item = {}
item['name'] = skill.xpath('./td[1]/text()')[0]# 技能
item['grade'] = skill.xpath('./td[2]/text()')[0]# 等级
item['property'] = skill.xpath('./td[3]/text()')[0]# 属性
item['type'] = skill.xpath('./td[4]/text()')[0]# 类型
item['target'] = skill.xpath('./td[5]/text()')[0]# 目标
item['power'] = skill.xpath('./td[6]/text()')[0]# 威力
item['pp'] = skill.xpath('./td[7]/text()')[0]# pp
item['result'] = skill.xpath('./td[8]/text()')[0]# 效果
industry.append(item)
# print(industry)
# 数据保存 (mysql)
sql = '''insert into pets(name,src,industry) values (%s,%s,%s);'''
cursor.execute(sql, [name, src, str(industry)])
conn.commit()
print(f'{name}--保存成功!')
except Exception as e:
pass
cursor.close()
conn.close()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

推荐阅读