Reputation: 183
from bs4 import BeautifulSoup
import requests
from fake_useragent import UserAgent
from xlsxwriter import Workbook
ua = UserAgent()
header = {'user_agent':ua.chrome}
main_url = 'https://www.ebay.com/sch/i.html?_from=R40&_trksid=m570.l1313&_nkw=laptop&_sacat=0'
page = requests.get(main_url, headers=header)
soup = BeautifulSoup(page.content, 'lxml')
links = soup.find_all('div', class_='s-item__info clearfix')
for item in links:
workbook = Workbook('Data.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write(0, 0, item.h3.text)
worksheet.write(0, 1, item.a['href'].split('?')[0])
worksheet.write(0, 2, item.div.next_sibling.next_sibling.text)
worksheet.write(0, 3, item.div.next_sibling.next_sibling.next_sibling.text)
worksheet.write(0, 4, item.find('span', class_='s-item__price').text)
workbook.close()
Upvotes: 0
Views: 54
Reputation: 22440
Try the script below. It should write the data in an excel file accrodingly. I almost change nothing in order to keep your basic structure intact. Make sure to change IDK
according to it's appropriate header names.
import requests
from bs4 import BeautifulSoup
from xlsxwriter import Workbook
from fake_useragent import UserAgent
ua = UserAgent()
header = {'user_agent':ua.chrome}
workbook = Workbook('Data.xlsx')
worksheet = workbook.add_worksheet(name="ItemList")
main_url = 'https://www.ebay.com/sch/i.html?_from=R40&_trksid=m570.l1313&_nkw=laptop&_sacat=0'
page = requests.get(main_url, headers=header)
soup = BeautifulSoup(page.content, 'lxml')
#creating headers
bold = workbook.add_format({"bold":1})
worksheet.write("A1","Name",bold)
worksheet.write("B1","Link",bold)
worksheet.write("C1","IDK",bold)
worksheet.write("D1","IDK",bold)
worksheet.write("E1","Price",bold)
row = 1
col = 0
for item in soup.find_all('div', class_='s-item__info clearfix'):
worksheet.write(row,col, item.h3.text)
worksheet.write(row,col+1, item.a['href'].split('?')[0])
worksheet.write(row,col+2, item.div.next_sibling.next_sibling.text)
worksheet.write(row,col+3, item.div.next_sibling.next_sibling.next_sibling.text)
worksheet.write(row,col+4, item.find('span',class_='s-item__price').text)
row+=1
workbook.close()
Upvotes: 1