-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
54 lines (46 loc) · 2.08 KB
/
main.py
File metadata and controls
54 lines (46 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import threading
import advertools as adv
from requests_html import HTMLSession
from requests.exceptions import SSLError, ConnectionError
import csv
import time
import os
sitemap = adv.sitemap_to_df('https://www.storyly.io/sitemap.xml')
threads = list()
data = list()
total_page = len(sitemap['loc'])
thread_count = 3
sleep_time = 2 # if you got SSLError or connection error increase the sleep time according to your need.
def scrapper(url, sleep_time=sleep_time):
print(f"Thread {threading.current_thread().name} is processing {url}")
with HTMLSession() as session:
try:
response = session.get(url)
if response.status_code == 200:
title_xpath = response.html.xpath('//title//text()')
description_xpath = response.html.xpath('//meta[@name="description"]/@content')
canonical_xpath = response.html.xpath("//link[@rel='canonical']/@href")
title = title_xpath[0] if title_xpath else 'title missing'
description = description_xpath[0] if description_xpath else 'description missing'
canonical = canonical_xpath[0] if canonical_xpath else 'canonical missing'
else:
print(url, response.status_code, 'HTTP Status ERROR')
except SSLError and ConnectionError:
time.sleep(sleep_time)
scrapper(url)
else:
data.append({'url':url, 'title':title, 'description':description, 'canonical':canonical})
for loop in range(int(total_page / thread_count)):
for index in range(loop * thread_count, (loop + 1) * thread_count):
if index < total_page:
url = sitemap['loc'][index].strip()
thread = threading.Thread(target=scrapper, args=(url,))
threads.append(thread)
thread.start()
for t in threads:
t.join()
csv_path = os.path.join(os.getcwd(), 'scrapper-results.csv')
with open(csv_path, 'w', encoding='utf-8') as file:
csv_file = csv.DictWriter(file, fieldnames=['url', 'title', 'description', 'canonical'])
csv_file.writeheader()
csv_file.writerows(data)