0

I've written a scraper in python to parse different names from a webpage. As I'm very new to panda so can't understand how to write data in a csv file properly. The way I've written my script does scrape desired names but in case of writing to a csv file it only write the last name. Any help on this will be vastly appreciated. Thanks in advance.

import requests ; from bs4 import BeautifulSoup
import pandas as pd

res = requests.get('https://bytes.com/topic/python/answers/596974-socket-error-10061-connection-refused').text
soup = BeautifulSoup(res,"lxml")
for name in soup.select(".smallfont a"):
    item = pd.DataFrame([name.text])[0]
    item.to_csv('file.csv')
    print(item)

2 Answers 2

1

I'm not sure if this is your question, but the problem is that you are overwriting the dataframe in the loop. In this way, just the last data in the list will be recorded on csv file.

Try this code:

import requests ; from bs4 import BeautifulSoup
import pandas as pd

res = requests.get('https://bytes.com/topic/python/answers/596974-socket-
error-10061-connection-refused').text
soup = BeautifulSoup(res,"lxml")
items_list = []
for name in soup.select(".smallfont a"):
    items_list.append(name.text)

item = pd.DataFrame(items_list)
item.to_csv('file.csv')
print(item)

Out put:

                                                   0
0  boost::asio (Permission denied, Connection ref...
1               Error Code 10061: Connection refused
2                    socket.error connection refused
3              Connection refused error from browser
4  connection refused when uploading a package to...
5                smtplib (111, 'Connection refused')
6  sending of mail (smtp) - connection refused - ...
7              socket object, connection refused ...
8                                 socket error 10061

Is this your intention?

Sign up to request clarification or add additional context in comments.

1 Comment

Yep it is. Thanks Rafael Araújo.
0

Why do you need panda ?

with open('file.csv', 'w+') as f:
    for name in soup.select(".smallfont a"):
        item = name.decode_contents()
        f.write(item)
        print(item)

1 Comment

Thanks Loïc, for your answer. I actually know how to write in a csv file if it were not for panda. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.