Skip to content

Commit 87de00d

Browse files
committed
web scrapper added
1 parent 3677988 commit 87de00d

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import requests
2+
from bs4 import BeautifulSoup as bs4
3+
import csv
4+
5+
6+
7+
def request_maker():
8+
headers = {
9+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
10+
}
11+
url = 'https://books.toscrape.com/index.html'
12+
res = requests.get(url, headers=headers)
13+
soup = bs4(res.content, 'html5lib')
14+
return soup
15+
16+
def finder(soup):
17+
books=[]
18+
prices = []
19+
h3 = soup.find_all('h3')
20+
price_tags = soup.find_all('p', class_='price_color')
21+
22+
for price_tag in price_tags:
23+
price = price_tag.text.strip()
24+
prices.append(price)
25+
26+
for book in h3:
27+
i = book.find('a').get('title')
28+
books.append(i)
29+
return books,prices
30+
31+
def csv_maker(data):
32+
books,prices = data
33+
file_name = 'book_data.csv'
34+
with open(file_name,'w') as csv_file:
35+
writer = csv.writer(csv_file)
36+
writer.writerow(['Title', 'Price'])
37+
for i in range(0,len(books)):
38+
writer.writerow([books[i],prices[i]])
39+
print("csv file generated successfully, saved as book_data.csv")
40+
41+
42+
def main():
43+
soup = request_maker()
44+
data = finder(soup)
45+
csv_maker(data)
46+
47+
if __name__ == '__main__':
48+
main()

0 commit comments

Comments
 (0)