-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlecon neuf_Web scraping using BeautifulSoup.py
More file actions
70 lines (53 loc) · 2.15 KB
/
lecon neuf_Web scraping using BeautifulSoup.py
File metadata and controls
70 lines (53 loc) · 2.15 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
print("Web Scrapping using BeautifulSoup")
# to use BeautifulSoup install it through cmd
# 1st install requests - pip install requests
# 2nd install BeautifulSoup - pip install BeautifulSoup4
import requests
from bs4 import BeautifulSoup
response = requests.get("https://www.jumia.co.ke/catalog/?q=finance+novels")
print(response) #<Response [200]> - means we have got a response from the server
# print(response.content)
print(response.status_code) #proceed if its 200, 404 means server not found
soup = BeautifulSoup(response.content, "html.parser")
# # print(soup)
#print(soup.prettify())
# find('a')
# find_all("img"), div
# find_parent("a")
# find_next_siblings("a")
# card = soup.find_all("img")
cards = soup.find_all("div", attrs = {"class":""})
for card in cards:
price = card.find("div",attrs = {"class":""})
# print(price.text)
title = card.find("span",attrs = {"class":""})
# print(title.text.strip("\n"))
pages = card.find("div",attrs = {"class":""})
# print(pages.text)
author = card.find("div",attrs = {"class":""})
# print(author.text)
# data = "{} {} {} {}".format(price.text,title.text.strip("\n"),pages.text,author.text)
data = "{} {} {} {}".format(price,title,pages,author) #prints none because i have not identified any container or class
print(data, "\n")
# Database access
# sqlite
import sqlite3
# sqlite3.connect("kibepy.db")
conn = sqlite3.connect("kibepy.db")
c = conn.cursor()
# c.execute("""CREATE TABLE IF NOT EXISTS EMP(ID INT PRIMARY KEY,NAME TEXT, SALARY REAL) """)
# c.execute("INSERT INTO EMP(ID,NAME,SALARY)VALUES(101,'ABC',100000)")
# c.execute("INSERT INTO EMP(ID,NAME,SALARY)VALUES(102,'EFG',200000)")
# c.execute("INSERT INTO EMP(ID,NAME,SALARY)VALUES(103,'HIJ',150000)")
# c.execute("INSERT INTO EMP(ID,NAME,SALARY)VALUES(104,'KLM',100500)")
# conn.execute("COMMIT")
c.execute("""SELECT * FROM EMP""")
# print(next(c))
for row in c:
print(row)
c.execute("UPDATE EMP SET SALARY = 650000 WHERE ID =102")
conn.execute("COMMIT")
c.execute("""SELECT * FROM EMP WHERE ID = 102""")
# print(next(c))
c.execute("DELETE FROM EMP WHERE ID = 101")
conn.execute("COMMIT")