import requests
import lxml.html
import csv
url = 'https://ssr1.scrape.center/'
source = requests.get(url).content
selector = lxml.html.fromstring(source)
item_list = selector.xpath('//*[@id="index"]/div[1]')
item_dict_list = []
a = 0
while a < 10:
for item in item_list:
show_name = item.xpath('//*[@id="index"]/div[1]/div[1]/div/div/div/div[2]/a/h2/text()')
show_score = item.xpath('//*[@id="index"]/div[1]/div[1]/div[1]/div/div/div[3]/p[1]/text()')
show_length = item.xpath('//*[@id="index"]/div[1]/div[1]/div[2]/div/div/div[2]/div[2]/span[3]/text()')
show_time = item.xpath('//*[@id="index"]/div[1]/div[1]/div[1]/div/div/div[2]/div[3]/span/text()')
show_country = item.xpath('//*[@id="index"]/div[1]/div[1]/div[1]/div/div/div[2]/div[2]/span[1]/text()')
item_dict = { 'show_name': show_name[a] if show_name else '',
'show_score': show_score[0] if show_score else '',
'show_length': show_length[0].strip() if show_length else '',
'show_time': show_time[0] if show_time else '',
'show_country':show_country[0] if show_country else ''}
item_dict_list.append(item_dict)
a += 1
if a == 11:
break
print(item_dict_list)
with open('result.csv', 'w', encoding='UTF-8') as f:
writer = csv.DictWriter(f,fieldnames=['show_name',
'show_score',
'show_length',
'show_time',
'show_country'])
writer.writeheader()
writer.writerows(item_dict_list)