Advertisement
AyanUpadhaya

Web Scraping and Excel Automation with Python

May 25th, 2021
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.96 KB | None | 0 0
  1. #Daily Star News Site Scraping Program
  2. #import all the required libraries
  3. #create a blank work book
  4. #we want scraping to be store in an excel file
  5. #get the current date
  6. #crete a new sheet
  7. #use iteration to store data
  8. #save the workbook
  9. #Script Written By : Ayan Upadhaya, contact: ayanU881@gmail.com
  10.  
  11. import requests
  12.  
  13. import openpyxl
  14.  
  15. from bs4 import BeautifulSoup as bs
  16.  
  17. from datetime import datetime
  18.  
  19. """WEB SCRAPING"""
  20. base_link="https://www.thedailystar.net/"
  21. response=requests.get(base_link).content
  22. soup=bs(response,"html.parser")
  23. data=soup.find_all('div',class_="list-content")
  24.  
  25.  
  26.  
  27. """EXCEL AUTOMATION"""
  28.  
  29. wb=openpyxl.Workbook()
  30.  
  31. current=datetime.date(datetime.now())
  32.  
  33. wb.create_sheet(index=1,title=str(current))
  34.  
  35. sheet=wb[str(current)]
  36.  
  37. sheet['A1']="Title"
  38. sheet['B1']="URL"
  39.  
  40. i=2
  41. for collections in data:
  42.     title=collections.h5.text
  43.     url=collections.h5.a.get('href')
  44.     sheet['A'+str(i)]=title
  45.     sheet['B'+str(i)]=base_link+url
  46.  
  47.     i+=1
  48.  
  49. wb.save('star.xlsx')
  50.  
  51. print("Success!")
  52.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement