I have written a Python script to scrape some products, so far i am able to get everything i need but i am stuck at how to save these values in csv.
Here is my Python code:
import requests
from bs4 import BeautifulSoup
mainPage = requests.get("http://mega.pk/mobiles/")
soup = BeautifulSoup(mainPage.content, "html5lib")
for link in soup.select("ul.asidemenu_h1 a[href*=http://www.mega.pk/mobiles-]"):
#link.get_text()
urls = [link.get('href')]
for url in urls:
brandPage = requests.get(url)
soup = BeautifulSoup(brandPage.content, "html5lib")
for productPage in soup.select("div.lap_thu_box div.image a[href*=http://www.mega.pk/mobiles_products/]"):
productUrls = [productPage.get('href')]
for productUrl in productUrls:
productPage = requests.get(productUrl)
soup = BeautifulSoup(productPage.content, "html5lib")
for productName in soup.select("div.col-md-8.col-sd-8.col-xs-8 div.padding-10 div h2 span"):
print (productName.get_text())
for productDesc in soup.select("div#main1 div div div div.row div div p"):
print (productDesc.get_text())
for productPrice in soup.select("div#main1 div div div div.row div div div div.price-n-action div span.desc-price"):
print (productPrice.get_text())
and please do tell me how to improve my code, I am pretty new to python. Before i wrote this script i used scrappy which was very fast (took 1 minute to do all) but I want to use python-3 (which takes at least 5-7 minutes). Though time is not the main issue. Saving elements in csv is important.