0
点赞
收藏
分享

微信扫一扫

python2爬取div数据

生活记录馆 2022-06-20 阅读 46
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 条评论