-
Notifications
You must be signed in to change notification settings - Fork 0
/
toi spider scraping code.py
40 lines (31 loc) · 1.25 KB
/
toi spider scraping code.py
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
import scrapy
import csv
class ToiSpider(scrapy.Spider):
name = 'toispi'
allowed_domains = ['timesofindia.indiatimes.com']
#domains that are allowed to scrape
with open("urls.txt","r") as f:
start_urls = [url.strip() for url in f.read().split("\n")]
#start_urls = ['https://timesofindia.indiatimes.com/topic/Winter-Olympics']
#urls it will visit
#name, allowed_domains, start_urls
print(start_urls)
#parse function: gets html of the page or css
#we are using css
def parse(self, response):
url = response.url
title = response.css("h1 span::text").get()
#urls = response.css("div.row a::attr(href)").getall()
date = response.css("div.byline span::text").get()
text = response.css("div._3YYSt::text").getall()
text = [x.strip() for x in text]
text = " ".join(text)
DELIM = "~"
csv_row = [url,title,date,text]
def clean_row(row):
return [item.strip().replace(DELIM," ")if item else " " for item in row]
with open("toiscraperesults.csv","a",newline="") as f:
writer = csv.writer(f,delimiter = DELIM)
writer.writerow(clean_row(csv_row))
#print("\n\n")
#print(url,title,date,text)