python爬虫文件代码大全-Python爬虫爬数据写入到文件

#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(' ')

#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文本,再慢慢导进数据库吧。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值