Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
writing data to a csv-file
#2
Code like this:
    a = t[0]
    b = t[1]
    c = t[2]
    d = t[3]
    e = t[4]
    f = t[5]
    g = t[4]
Code like this is an indication that the design is not correct. Use data structures to represent your data.
You're assigning elements from a list to names and then you create from this a new list.


Your data is 2-dimensional. The first dimension is the index (rows) and the second dimension are the columns (td-data).

Create an empty list, which is later your whole dataset.
For each tag you need the text or an attribute. Putting a whole tag object into pandas will not work.

td_results = []
for i in range(1, 100):
    url = "https://my-website.com/webid={}".format(i)
    s = session.get(url, headers=headers, cookies=cookies)
 
    soup = bs(s.text, 'html.parser')
    data = soup.find_all('td') 
    td_results.append(column.text for column in soup.find_all('td')) # <- this here is the critical part
    # he could find something or not
    # and the amount of td elements can be different


print(td_results)
df = pdDataFrame(td_results)
So if you know all pages do have the same structure and you know for example, that you need the first 10 element, then you can use the subscription method.

Example to get the first 10 elements:
td_results.append(column.text for column in soup.find_all('td')[:10])
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
writing data to a csv-file - by apollo - Jul-03-2020, 02:08 PM
RE: writing data to a csv-file - by DeaD_EyE - Jul-03-2020, 02:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems writing a large text file in python Vilius 4 1,157 Dec-21-2024, 09:20 AM
Last Post: Pedroski55
  writing list to csv file problem jacksfrustration 5 2,674 Jul-04-2024, 08:15 PM
Last Post: deanhystad
  Help with writing monitored data to mysql upon change of one particular variable donottrackmymetadata 3 1,584 Apr-18-2024, 09:55 PM
Last Post: deanhystad
  Issue in writing sql data into csv for decimal value to scientific notation mg24 8 6,023 Dec-06-2022, 11:09 AM
Last Post: mg24
  Create a function for writing to SQL data to csv mg24 4 2,691 Oct-01-2022, 04:30 AM
Last Post: mg24
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 4,036 Sep-27-2022, 01:38 PM
Last Post: buran
  Writing to json file ebolisa 1 1,785 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Writing to External File DaveG 9 4,730 Mar-30-2022, 06:25 AM
Last Post: bowlofred
  Need Help writing data into Excel format ajitnayak87 8 4,269 Feb-04-2022, 03:00 AM
Last Post: Jeff_t
  Fastest Way of Writing/Reading Data JamesA 1 3,043 Jul-27-2021, 03:52 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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