import pymysql conn=pymysql.connect(host='127.0.0.1',port=3306,db='test',passwd='NAVJ-W56S-3YUU-MVHV',user='root',charset='utf8') cursor=conn.cursor() sql='''create table if not exists jin(date varchar(20),week varchar(20), weather varchar(20),low varchar(20),high varchar(20)) charset=utf8''' cursor.execute(sql) cursor.execute('show tables') import requests from lxml import html url='https://www.tianqi.com/jintaiqu/15/' html_code=requests.get(url=url) html_code.encoding='utf-8' html_text=html_code.text etree_tools=html.etree format_html=etree_tools.HTML(html_text) li_lists=format_html.xpath('//*[@class="weaul"]/li/a') for node in li_lists: da=node.xpath('./div[1]/span[1]/text()') wee=node.xpath('./div[1]/span[2]/text()') wea=node.xpath('./div[3]/text()') lo=node.xpath('./div[4]/span[1]/text()') hig=node.xpath('./div[4]/span[2]/text()') print(da) print(wee) print(wea) print(lo) print(hig) for k in range(len(da)): sql="insert into jin (date,week,weather,low,high) values(%s,%s,%s,%s,%s)" cursor.execute(sql,(da[k],wee[k],wea[k],lo[k],hig[k])) conn.commit() data=cursor.execute('select * from jin') data=cursor.fetchall() print(data) conn.close()
da、wee、lo、hig这几个参数没在for循环定义,在li_lists的循环外使用。可以把插入数据库操作放到li_lists的循环内。或者把这几个变量在循环外定义成数组,循环把数据加入到数组中。