python2爬取div数据

import requests
import parsel
import csv
from lxml import etree
import sys
reload(sys)
sys.setdefaultencoding('utf8')
f = open('/opt/gopath/src/pyscrPage/testBlock.csv', mode='a+')
csv_writer= csv.DictWriter(f,fieldnames=['Block Miner Uncle Rewards'])
csv_writer= csv.DictWriter(f,fieldnames=['UncleBlockMiner','UncleBlock','UncleBlockRewards','Time','InclusionBlock','InclusionBlockMiner','UncleHeightBlockMiner','UncleHeightBlockReward','UncleHeightBlockTxCount','UncleHeightBlockGasUsage','UncleHeightBlockBaseFee','PrevBlockMiner','PrevBlockReward','PrevBlockTxCount','PrevBlockGasUsage','PrevBlockBaseFee'])
csv_writer.writeheader()
urlUncle = "https://cn.etherscan.com/uncles?m=0xea674fdde714fd979de3edf0f56aa9716b898ec8&ps=10&p={}"
headers = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62'}
urlBlock = "https://cn.etherscan.com/block/{}"
i=1
while i<2:
    url = urlUncle.format(i)
    response = requests.get(url=url, headers=headers)
    selector = parsel.Selector(response.text)
    trs = selector.xpath('//table[@class="table table-hover"]/tbody/tr')
    for tr in trs:
        UncleBlockMiner = tr.xpath('./td[5]/a/text()').get()
        UncleBlock = tr.xpath('./td[2]/a/text()').get()
        UncleBlockRewards = tr.xpath('./td[6]').xpath("string(.)").extract()
        Time = tr.xpath('./td[3]/span/text()').get()
        InclusionBlock = tr.xpath('./td[1]/a/text()').get()
        url2 = urlBlock.format(InclusionBlock)
        response = requests.get(url=url2, headers=headers)
        selector = parsel.Selector(response.text)
        InclusionBlockMinerDiv = selector.xpath('//div[@class="row align-items-center"]')[2]
        InclusionBlockMiner = InclusionBlockMinerDiv.xpath('./div[2]/a/text()').get()
        url3 = urlBlock.format(UncleBlock)
        response = requests.get(url=url3, headers=headers)
        selector = parsel.Selector(response.text)
        UncleHeightBlockMinerDiv = selector.xpath('//div[@class="row align-items-center"]')[2]
        UncleHeightBlockMiner = UncleHeightBlockMinerDiv.xpath('./div[2]/a/text()').get()
        UncleHeightBlockRewardDiv = selector.xpath('//div[@class="row align-items-center"]')[3]
        UncleHeightBlockReward = UncleHeightBlockRewardDiv.xpath('./div[2]').xpath("string(.)").extract()
        UncleHeightBlockTxCountDiv = selector.xpath('//div[@class="row align-items-center"]')[1]
        UncleHeightBlockTxCount = UncleHeightBlockTxCountDiv.xpath('./div[2]/a[1]/text()').get()
        UncleHeightBlockGasUsageDiv = selector.xpath('//div[@class="row align-items-center"]')[8]
        UncleHeightBlockGasUsage = UncleHeightBlockGasUsageDiv.xpath('./div[2]/text()').get()
        UncleHeightBlockBaseFeeDiv = selector.xpath('//div[@class="row align-items-center"]')[10]
        UncleHeightBlockBaseFee = UncleHeightBlockBaseFeeDiv.xpath('./div[2]').xpath("string(.)").extract()
        url4 = urlBlock.format(int(UncleBlock) - 1)
        response = requests.get(url=url4, headers=headers)
        selector = parsel.Selector(response.text)
        PrevBlockMinerDiv = selector.xpath('//div[@class="row align-items-center"]')[2]
        PrevBlockMiner = PrevBlockMinerDiv.xpath('./div[2]/a/text()').get()
        PrevBlockRewardDiv = selector.xpath('//div[@class="row align-items-center"]')[3]
        PrevBlockReward = PrevBlockRewardDiv.xpath('./div[2]').xpath("string(.)").extract()
        PrevBlockTxCountDiv = selector.xpath('//div[@class="row align-items-center"]')[1]
        PrevBlockTxCount = PrevBlockTxCountDiv.xpath('./div[2]/a[1]/text()').get()
        PrevBlockGasUsageDiv = selector.xpath('//div[@class="row align-items-center"]')[8]
        PrevBlockGasUsage = PrevBlockGasUsageDiv.xpath('./div[2]/text()').get()
        PrevBlockBaseFeeDiv = selector.xpath('//div[@class="row align-items-center"]')[10]
        PrevBlockBaseFee = PrevBlockBaseFeeDiv.xpath('./div[2]').xpath("string(.)").extract()
        dic={'UncleBlockMiner':UncleBlockMiner,'UncleBlock':UncleBlock,'UncleBlockRewards':UncleBlockRewards,'Time':Time,'InclusionBlock':InclusionBlock,'InclusionBlockMiner':InclusionBlockMiner,'UncleHeightBlockMiner':UncleHeightBlockMiner,'UncleHeightBlockReward':UncleHeightBlockReward,'UncleHeightBlockTxCount':UncleHeightBlockTxCount,'UncleHeightBlockGasUsage':UncleHeightBlockGasUsage,'UncleHeightBlockBaseFee':UncleHeightBlockBaseFee,'PrevBlockMiner':PrevBlockMiner,'PrevBlockReward':PrevBlockReward,'PrevBlockTxCount':PrevBlockTxCount,'PrevBlockGasUsage':PrevBlockGasUsage,'PrevBlockBaseFee':PrevBlockBaseFee}
        csv_writer.writerow(dic)
    i=i+1

nohup python2  BlockTest.py  >>MinerUncleInfo.log 2>&1 &
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值