Python Forum
Capturing BS4 values into DF and writing to CSV
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Capturing BS4 values into DF and writing to CSV
#1
I have the below logic that im getting results from, but when righting to the csv, each data value is being written to row instead of collectively into columns and rows.

import requests
from bs4 import BeautifulSoup
import pandas as pd

productdetails = []

headers = {
    'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36'
}

df2 = pd.DataFrame(columns=['Description', 'Price1', 'Price2'],index=range(1))

for x in range(1,180):
    response = requests.get(f'https://www.site.com/c/mens/mens-footwear?&page_{x}', verify=False, headers=headers)
    soup = BeautifulSoup(response.content,'lxml')

    element_list = soup.find_all('div',class_='product-content')
    for element in element_list:
        for link in element.find_all('a', class_='product-card-simple-title'):
            productdetails.append("Description: " + link.get_text().strip())
            for price in element.find_all('span',class_='sr-only'):
                productdetails.append("Price1: " + price.get_text().strip().replace('\n', '').replace(' ','').replace('dollars','.').replace('cents',''))
                if len(element.find_all('span',class_='sr-only')) == 2:
                    productdetails.append("Price2: " + price.get_text().strip().replace('\n', '').replace(' ','').replace('dollars','.').replace('cents',''))

print(productdetails)
Im currently working on checking if there are more than 1 prices, so that i can add that as 2nd price in the dataframe.


CSV data currently:
0,Description: Brooks Men's Adrenaline GTS 23 Running Shoes
1,Price1: 139.99
2,Description: Nike Men's Revolution 6 Next Nature Running Shoes
3,Price1: 22.37
4,Price2: 44.97
Expected CSV data:
Description, Price1, Price2
Brooks Men's Adrenaline GTS 23 Running Shoes, 139.99,
Nike Men's Revolution 6 Next Nature Running Shoes,22.37,44.97
Reply


Messages In This Thread
Capturing BS4 values into DF and writing to CSV - by cubangt - Aug-29-2023, 07:52 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  capturing multiline output for number of parameters jss 3 871 Sep-01-2023, 05:42 PM
Last Post: jss
  Json filter is not capturing desired key/element mrapple2020 1 1,200 Nov-24-2022, 09:22 AM
Last Post: ibreeden
  Capturing inputs values from internal python script limors11 11 5,321 Jun-16-2019, 05:05 PM
Last Post: DeaD_EyE
  Capturing a snapshot from the video sreeramp96 1 2,211 May-24-2019, 07:02 AM
Last Post: heiner55
  HTTP response capturing issue miunika 1 2,078 Mar-16-2019, 01:46 PM
Last Post: Larz60+
  HTTP response capturing issue anna 2 2,556 Mar-15-2019, 03:08 PM
Last Post: Larz60+
  Capturing error from sql Grego 1 2,486 Jun-29-2018, 11:17 AM
Last Post: ichabod801
  parsing values and writing back in xml file deepa 4 3,979 Sep-11-2017, 09:07 AM
Last Post: deepa
  Writing values at a desired column in a line of text file Gupta 3 3,539 Jul-28-2017, 11:08 PM
Last Post: Larz60+
  Can PyAudio (Port Audio) validly accept float values when writing to stream? cdrandin 1 3,833 Mar-26-2017, 07:54 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020