简体   繁体   中英

How to extract data from the HTML using python?

How to extract the data from from the html?

from urllib.request import urlopen
url = 'http://book.ponniyinselvan.in/part-1/chapter-1.html'
page = urlopen(url)

getting HTTPError: HTTP Error 403: Forbidden

I am trying to extract the data into CSV file.

You can use this example how to save the text into a CSV file:

import csv
import requests
from bs4 import BeautifulSoup

url = "http://book.ponniyinselvan.in/part-1/chapter-1.html"

with open("data.csv", "w") as f_out:
    writer = csv.writer(f_out)

    soup = BeautifulSoup(requests.get(url).content, "html.parser")
    text = soup.section.get_text(strip=True, separator="\n")

    writer.writerow(["Chapter", "Text"])
    writer.writerow([1, text])

Saves data.csv (screenshot from LibreOffice):

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM