-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractor1.py
30 lines (24 loc) · 1.35 KB
/
extractor1.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
import requests
with open("quotes.csv", "w") as file:
pass
for i in range(1,11):
r=requests.get(f"https://quotes.toscrape.com/page/{i}")
text=r.text
print(r.status_code)
with open ("quotes.csv",'a',encoding='utf-8') as f:
# for line in text.split('\n'):
# if '<span>by <small class="author" itemprop="author">' in line :
# line=line.replace('<span>by <small class="author" itemprop="author">','').replace('</small>','').strip()
# f.write(line)
# for line in text.split("\n"):
# if '<span class="text" itemprop="text">' in line :
# line=line.replace('<span class="text" itemprop="text">“','').replace('”</span>','').strip()
# f.write(line)
# f.write('\n')
for line in text.split('\n'):
if '<span class="text" itemprop="text">' in line :
quotes=line.replace('<span class="text" itemprop="text">“','').replace('”</span>','').strip()
if '<span>by <small class="author" itemprop="author">' in line :
author=line.replace('<span>by <small class="author" itemprop="author">','').replace('</small>','').replace(",","|").strip()
f.write(author+","+quotes)
f.write("\n")