iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python爬虫爬数据写入到文件
  • 145
分享到

Python爬虫爬数据写入到文件

爬虫文件数据 2023-01-31 03:01:36 145人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

#coding=utf-8 import requests from bs4 import BeautifulSoup import sys reload(sys)   sys.setdefaultencoding('utf8')  r=r

#coding=utf-8
import requests
from bs4 import BeautifulSoup
import sys
reload(sys)  
sys.setdefaultencoding('utf8') 
r=requests.get('Http://html-color-codes.info/color-names/')
html=r.text
#print html
soup=BeautifulSoup(html,'html.parser')
trs=soup.find_all('tr')
f=open('color.txt','a')
index=1
for tr in trs:
	style=tr.get('style')
	tds=tr.find_all('td')
	td=[x for x in tds]
	name=td[1].text.strip()
	hex=td[2].text.strip()
	string=str(index)+','+name+','+hex+','+style
	f.write(string)
	f.write('\r\n')
	#print('序号:'+str(index)+'颜色:'+name+'颜色值:'+hex+'背景色样式'+style)
	index=index+1
f.close()
'''
for index in range(len(trs)):
	style=trs[index].get('style')
	tds=trs[index].find_all('td')
	name=tds[1].text
	hex=tds[2].text
	print('颜色:'+name+'颜色值:'+hex+'背景色样式'+style)
'''

直接上代码。本来这次是想抓取数据直接通过Mysql相关的包写入到数据库来着,结果在网上找教程的时候发现mysql那玩意好难安装。。。。。所以就直接放弃了。间接的把数据先写进txt文本,再慢慢导进数据库吧。。。。

--结束END--

本文标题: Python爬虫爬数据写入到文件

本文链接: https://www.lsjlt.com/news/187283.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作