[英]How to extract data from the HTML using python?
How to extract the data from from the html?如何从 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.我正在尝试将数据提取到 CSV 文件中。
You can use this example how to save the text into a CSV file:您可以使用此示例如何将文本保存到 CSV 文件中:
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):保存data.csv
(来自 LibreOffice 的屏幕截图):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.