import requests
from lxml import etree
header = {'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'}
url = 'https://www..com/stats/global/yearly/g_population_total/'
for i in range(1990,2019):
page_url = url + str(i) + '.html'
page_text = requests.get(url=page_url,headers=header).text
tree = etree.HTML(page_text)
tr_list = tree.xpath('/html/body/div[2]/div[1]/div[4]/div[1]/div/div/div/table/tbody')
for nn in tr_list:
sn = nn.xpath('./tr[not(@class="tr_region")]/td/text()')
dataa = [x.strip() for x in sn if x.strip() != '']
with open('out.txt','a',encoding='utf8',newline='\r\n') as f:
for q in dataa:
f.write(q+'|')
if q.endswith("%"):
f.write(str(i)+'\r\n')
|