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()
怎么能够把数据爬取出来,我搞错了啥?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- storm_huang 2020-12-29 17:54关注
da、wee、lo、hig这几个参数没在for循环定义,在li_lists的循环外使用。可以把插入数据库操作放到li_lists的循环内。或者把这几个变量在循环外定义成数组,循环把数据加入到数组中。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
- ¥20 怎么用dlib库的算法识别小麦病虫害
- ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
- ¥15 java写代码遇到问题,求帮助
- ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
- ¥15 有了解d3和topogram.js库的吗?有偿请教
- ¥100 任意维数的K均值聚类
- ¥15 stamps做sbas-insar,时序沉降图怎么画
- ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
- ¥15 关于#Java#的问题,如何解决?