Understanding Python List Handling and CSV Output

Asked 1 years ago, Updated 1 years ago, 288 views

I am having trouble understanding the handling of the list and CSV output.

I am learning to export the scraping results to CSV, but I cannot export the list to CSV with the code sample_02.py.

Please let me know how I can write to CSV.

I think the writer.writerow([em.text,em.get("href")) section contains the retrieved text and links. Do you want to combine [em.text,em.get("href") with commas?Does that mean it's separated?

#sample_01.py

import csv
import requests
from bs4 import BeautifulSoup

url="https://www.yahoo.co.jp/"
res=requests.get(url)

soup = BeautifulSoup(res.content, "html.parser")

topology=soup.find(id="Topics")

# a Write the text portion of the tag and the URL obtained in href in CSV format
with open("info_01.csv", "w", newline="", encoding="utf_8") asw:
    writer=csv.writer(w)
    for element in topics.find_all("a"):
        writer.writerow([elem.text, elem.get("href")])

Run Results (Successful)

 Extraordinary Diet Bill passed 95.5% NEW, https://news.yahoo.co.jp/pickup/6447265
Victims Relief Act enacted, https://news.yahoo.co.jp/pickup/6447261
Next week, the cold will be severe and there will be snow in western Japan, https://news.yahoo.co.jp/pickup/6447258

Next, to learn how to handle arrays, I tried to put them in an array and write them out in one go, such as elem.text and elem.get("href"), but they didn't work.

#sample_02.py

tag_li = [ ]
link_li = [ ]
with open("info-2.csv", "w", newline=", encoding="utf_8") as w:
    writer=csv.writer(w)
    for element in topics.find_all("a"):
        tag_li.append(elem.text)
        link_li.append(em.get("href"))
    
    writer.writerows ([tag_li, link_li])

Execution Results (Failed Why??)

 Extraordinary Diet bill enacted 95.5% NEW, victim relief law enacted, cold weather in western Japan next week, Russia, Iran and drone production review NEW, factory restricted near yellow gas leak, 50s, noisy? NEW, Shirafu World Cup site, 16:02 Kyodo News, see more, topics list
https://news.yahoo.co.jp/pickup/6447265,https://news.yahoo.co.jp/pickup/6447261,https://news.yahoo.co.jp/pickup/6447258,https://news.yahoo.co.jp/pickup/6447256,https://news.yahoo.co.jp/pickup/6447263,https://news.yahoo.co.jp/pickup/6447249,https://news.yahoo.co.jp/pickup/6447259,https://news.yahoo.co.jp/artic://news.yahoo.co.jp/pickup/6447264,https://news.yahoo.co.jp/articles/b58cff17ed19d2c0d5e64db996d0ebddc5f2d07a/images/000, https://news.yahoo.co.jp/topics/top-picks,https://news.yahoo.co.jp/topics

python python3 csv

2022-12-10 13:04

1 Answers

Take a simple program as an example.

import csv
importio

with io.StringIO() as fp:
    writer=csv.writer(fp)

    # writerow writes one line of
    writer.writerow (['line 1', 110, 120])

    # writerows writes multiple lines
    data = [['2nd line', 210, 220], ['3rd line', 310, 320]]
    writer.writerows (data)
    res=fp.getvalue()

print(res)
# Line 1, 110,120
# Line 2, 210,220
# Line 3, 310, 320

As long as you have the above structure,

writer.writerow([em.text,elem.get("href"))
If you're writing a line in , just save it as follows.

 lines=[ ]
for element in topics.find_all("a"):
    line = [elem.text, elem.get("href")]
    lines.append(line)


2022-12-10 15:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.