CSV文件中的数据一无所有

发布于 2025-02-04 23:00:42 字数 768 浏览 1 评论 0原文

当我将数据打印到数据框中时,他们将在我制作CSV文件时打印所有数据

import requests
from bs4 import BeautifulSoup
import pandas as pd

headers ={
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'
}
r =requests.get('https://web.archive.org/web/20141113155415/http://www.alexa.com/topsites/category/Top/Recreation/Living_History/By_Historical_Region')
soup=BeautifulSoup(r.content, 'lxml')
main=soup.find_all('div',class_='desc-container')
web={}
for mains in main:

    link=mains.find('p',class_='desc-paragraph').text
    web['link']=link
    des=mains.find('div',class_='description').text
    web['description']=des
    
    
    df = pd.DataFrame([web])
    print(df)

WHEN I PRINT the data into data frame they will print all the data when I make the csv file they will give me nothing in csv file

import requests
from bs4 import BeautifulSoup
import pandas as pd

headers ={
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'
}
r =requests.get('https://web.archive.org/web/20141113155415/http://www.alexa.com/topsites/category/Top/Recreation/Living_History/By_Historical_Region')
soup=BeautifulSoup(r.content, 'lxml')
main=soup.find_all('div',class_='desc-container')
web={}
for mains in main:

    link=mains.find('p',class_='desc-paragraph').text
    web['link']=link
    des=mains.find('div',class_='description').text
    web['description']=des
    
    
    df = pd.DataFrame([web])
    print(df)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流云如水 2025-02-11 23:00:42

尝试将带有数据的字典放入列表中,每次迭代都会创建新词典。例如:

import requests
import pandas as pd
from bs4 import BeautifulSoup

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36"
}
r = requests.get(
    "https://web.archive.org/web/20141113155415/http://www.alexa.com/topsites/category/Top/Recreation/Living_History/By_Historical_Region"
)

soup = BeautifulSoup(r.content, "lxml")
main = soup.find_all("div", class_="desc-container")

all_data = []
for mains in main:
    web = {}
    link = mains.find("p", class_="desc-paragraph").text
    des = mains.find("div", class_="description").text

    web["link"] = link
    web["description"] = des
    all_data.append(web)

df = pd.DataFrame(all_data)
print(df.to_markdown())
df.to_csv("data.csv", index=False)

打印:

链接说明
0Texrenfest.com得克萨斯州文艺复兴音乐节将于10月和11月在得克萨斯州的Planterville举行。
1Rennfest.com在Crownsville的一个16世纪英国村庄的娱乐活动。包括SCH ...更详尽的,就业,手工艺和供应商应用,照片,娱乐描述和方向。
2Plimoth.org提供有关Plimoth Plantation的信息,马萨诸塞州普利茅斯的第十七CEN生活历史博物馆。还可以作为一种资源,访问者可以通过这些资源了解有关朝圣故事,普利茅斯殖民地(1620-1692),Wampanoag印第安人,感恩节的更多信息,并找到了网络上相关网站的链接。
3Royalfaires.com/carolina/在北卡罗来纳州夏洛特举行的年度活动。包括活动时间表,票务信息,地图,A ... MORCODRICATIONS和联系信息。
4Renfestival.com位于俄亥俄州哈维斯堡。参观16世纪,参观杂耍,罗宾汉和伊利皇后...莫尔扎贝斯一世
5Kingrichardsfaire.net16世纪在马萨诸塞州卡佛的节日。通常运行至8月至10月中旬。
6Rileysfarm.comRiley's Farm是一个在南加州的苹果园和生活历史农场。安排... Morefield旅行,或阅读有关殖民圣诞节的信息。
7BostonMassacre.net包括有关活动的资源目录,包括图片,文档,位置,试用,Parti ... Morcipert和时间表。
8Renfair.com/ny/提供杂耍者,玩笑者,Minstrels和魔术师以及Jousts的持续表演...更多地是由骑士穿着装甲的骑士,在纽约州Tuxedo附近的Sterling Forest上充电。
9Piratemerch.com专门研究海盗时尚,衣服,T恤和礼物。
10Renfaire.com提供了语言,着装,风俗和活动的背景详细信息。
11Okcastle.com体验Castleton的喜悦;斗争,现场象棋比赛,蒙面球和国王...更多的盛宴。 5月的前三个周末加入亨利八世国王。
12Livinghistory.co.uk是英国重新制定者的资源。包括有关历史,服装和音乐,活动的信息...更多列表,论坛和免费的网站空间。
13nps.gov/casa提供有关可访问性,事件,教育计划,管理,新闻,历史的信息。
14ASF.NET每年吸引300,000多名游客,每年生产14种世界一流的作品,包括古典作品,现代戏剧,豪华的音乐剧以及由节日的南方作家项目委托进行的新作品。
15Kipar.org一个令人印象深刻的网站,其中包含有关法国和英国巴洛克时期的信息。
16Larp.com/legioxx/一世纪的英国古罗马军队和英国的平民生活被重演,以供公开... Morec事件和教育外展。总部位于华盛顿特区;一般和联系信息。
17Missionsanluis.org包含1656年至1704年佛罗里达州早期定居点和首都的材料。
18Ren-fest.com佛罗里达文艺复兴时期的制作人和Kyng的Company Renaissance Faire。
19Golondrinas.orgThe Swallows的牧场,这是新墨西哥州圣达菲的西班牙殖民历史博物馆。因此,在图片中可以看到莫雷达达斯(1820年左右)和殖民平民。
20Legionxxiv.org宾夕法尼亚州的团体重新制定了罗马军团。设备标准,活动时间表,P ...重新制定,背景信息有关罗马。
21Regia.orgAnglo-Saxon,Viking,Norman和英国生活历史950-1066AD。
22Pyracy.com一个迷恋海盗的在线社区。包括一个论坛和一些图像。
23Quartergiven.netGroup描绘了Faires,重演,博物馆会议和类似活动的海盗生活; M ...更多信息信息,活动时间表,图像库。
24佛罗里达州萨拉索塔战争马农场的Warhorse.com小组在Faires and S ...自1982年以来在Faires and S ...更像事件,使用比利时,Percheron,Shire和Clydesdale Horses和Clydesdale Horses和Clydesdale Horses以及16世纪的完整套件允许骑手进行真实的完整联系。活动时间表。

并保存data.csv(从libreoffice屏幕截图):

”在此处输入图像描述”

Try to put the dictionary with data into a list and each iteration create new dictionary. For example:

import requests
import pandas as pd
from bs4 import BeautifulSoup

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36"
}
r = requests.get(
    "https://web.archive.org/web/20141113155415/http://www.alexa.com/topsites/category/Top/Recreation/Living_History/By_Historical_Region"
)

soup = BeautifulSoup(r.content, "lxml")
main = soup.find_all("div", class_="desc-container")

all_data = []
for mains in main:
    web = {}
    link = mains.find("p", class_="desc-paragraph").text
    des = mains.find("div", class_="description").text

    web["link"] = link
    web["description"] = des
    all_data.append(web)

df = pd.DataFrame(all_data)
print(df.to_markdown())
df.to_csv("data.csv", index=False)

Prints:

linkdescription
0Texrenfest.comTexas Renaissance Festival runs October and November at Plantersville, Texas.
1Rennfest.comA recreation of a 16th century English village named Revel Grove in Crownsville. Includes a sch... Moreedule, employment, craft and vendor applications, photographs, entertainment descriptions, and directions.
2Plimoth.orgProvides information about Plimoth Plantation, the living history museum of the seventeenth cen... Moretury in Plymouth, Massachusetts. Also serves as a resource by which visitors may learn more about the Pilgrim Story, the history of Plymouth Colony (1620-1692), the Wampanoag Indians, Thanksgiving, and find links to related sites on the Web.
3Royalfaires.com/carolina/Annual event in Charlotte, North Carolina. Includes events schedule, ticket information, map, a... Moreccommodations, and contact information.
4Renfestival.comLocated in Harveysburg, OH. Visit the 16th Century and see Jousting , Robin Hood, and Queen Eli... Morezabeth I.
5Kingrichardsfaire.net16th Century festival in Carver, Massachusetts. Typically runs August through mid October.
6Rileysfarm.comRiley's Farm is a working Southern California apple orchard and living history farm. Arrange a ... Morefield trip, or read about Colonial Christmas.
7Bostonmassacre.netIncludes resource directory on the event, including pictures, documents, location, trial, parti... Morecipants, and timeline.
8Renfair.com/NY/Offers continuous performances by jugglers, jesters, minstrels and magicians, as well as jousts... More by knights in armor on charging horses located in Sterling Forest, near Tuxedo, NY.
9Piratemerch.comSpecializes in pirate fashions, clothing, T-shirts, and gifts.
10Renfaire.comProvides background details of language, dress, customs and activities.
11Okcastle.comExperience the delights of Castleton; jousting, live chess matches, a masked ball, and the King... More's feast. Join King Henry VIII the first three weekends of May.
12Livinghistory.co.ukA resource for British re-enactors. Includes information on history, costumes and music, events... More listings, forums and free webspace for re-anactors.
13Nps.gov/casaFeatures information on accessibility, events, educational programs, management, news, history ... Moreand culture.
14Asf.netAttracting more than 300,000 visitors a year, ASF produces 14 world-class productions annually,... More including classical works, contemporary plays, lavish musicals, and new works commissioned by the Festival's Southern Writers' Project.
15Kipar.orgAn impressive site containing information on both the French and English Baroque periods.
16Larp.com/legioxx/Ancient Roman army and civilian life in the Britain in the First Century is reenacted for publi... Morec events and educational outreach. Based in Washington, DC; general and contact information.
17Missionsanluis.orgContains materials from the early settlement and capital of La Florida from 1656 to 1704. It in... Morecludes on-line collection of materials from the museum, and directions.
18Ren-fest.comProducer of the Florida Renaissance Festival and the Kyng's Company Renaissance Faire.
19Golondrinas.orgThe Ranch of the Swallows, a Spanish Colonial living history museum in Santa Fe, New Mexico. So... Moreldados (1820 or so) and Colonial civilians can be seen in the pictures.
20Legionxxiv.orgPennsylvania-based group re-enacting a Roman Legion. Equipment standards, schedule of events, p... Morehotos of re-enactments, background information on Rome.
21Regia.orgAnglo-Saxon, Viking, Norman and British Living History 950-1066AD.
22Pyracy.comAn online community enamoured with pirates. Includes a forum and some images.
23Noquartergiven.netGroup portrays the pirate life for Faires, reenactments, museum meetings, and similar events; m... Moreembership information, events schedule, image gallery.
24Warhorse.comGroup from the War Horse Farm in Sarasota, Florida, have jousted professionally at Faires and s... Moreimilar events since 1982, using Belgian, Percheron, Shire, and Clydesdale horses and full suits of replica Sixteenth Century tilting armor which allows riders to engage in an authentic full contact joust. Events schedule.

and saves data.csv (screenshot from LibreOffice):

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文