Reputation: 95
I've created a script to parse the titles and their associated links from a webpage and write the same to an excel file using openpyxl library. The script is doing fine. However, what I can't do is draw border around the cells the results to be written.
I've tried so far:
import requests
from bs4 import BeautifulSoup
from openpyxl import Workbook
link = "https://stackoverflow.com/questions/tagged/web-scraping"
wb = Workbook()
wb.remove(wb['Sheet'])
def fetch_content(link):
req = requests.get(link)
soup = BeautifulSoup(req.text,"lxml")
for item in soup.select(".summary .question-hyperlink"):
title = item.get_text(strip=True)
post_link = item.get("href")
yield title,post_link
if __name__ == '__main__':
ws = wb.create_sheet("output")
ws.append(['Title','Link'])
row = 2
for title,post_link in fetch_content(link):
ws.cell(row=row, column=1).value = title
ws.cell(row=row, column=2).value = post_link
row+=1
wb.save("SO.xlsx")
How can I draw border around cells the results to be written?
Upvotes: 0
Views: 116
Reputation: 20042
Is this want you want?
import requests
from bs4 import BeautifulSoup
from openpyxl import Workbook
from openpyxl.styles.borders import Border, Side
link = "https://stackoverflow.com/questions/tagged/web-scraping"
wb = Workbook()
wb.remove(wb['Sheet'])
def fetch_content(link):
soup = BeautifulSoup(requests.get(link).text, "lxml")
for item in soup.select(".summary .question-hyperlink"):
yield item.get_text(strip=True), item.get("href")
if __name__ == '__main__':
thin_border = Border(
left=Side(style='thin'),
right=Side(style='thin'),
top=Side(style='thin'),
bottom=Side(style='thin'),
)
ws = wb.create_sheet("output")
ws.append(['Title', 'Link'])
row = 2
for title, post_link in fetch_content(link):
ws.cell(row=row, column=1).value = title
ws.cell(row=row, column=1).border = thin_border
ws.cell(row=row, column=2).value = post_link
ws.cell(row=row, column=2).border = thin_border
row += 1
wb.save("SO.xlsx")
Output:
Upvotes: 1