import requests
from bs4 import BeautifulSoup
import pandas as pd
URL = 'https://mychesterfieldschools.com/mams/news-and-announcements/'
page = requests.get(URL)
soup = BeautifulSoup(page.content, 'html.parser')
results = soup.find_all('div', class_='col-sm-12 col-md-12')
for results in results:
body = results.find('p')
link = results.find('a')
a = body.text
b = link
print(a)
print(b)字符串很奇怪,我对Python非常陌生。请帮帮我!我试过用潘达斯,但对我没用。
以下是我们想要的输出:支持学生的Chromebook到7月30日为止: Thomas星期二上午8-10点,星期四上午8-10点,CTC@Hull,2-4,...Read全文
这里有一些资源,让你的孩子在夏天从事数学,并为他们将在秋季进入的课程做准备。亚历克斯--你的孩子在本学年的数学课上一直在使用阿力克斯。阿列克斯是一个自适应的数学程序,为每个学生提供个性化的学习路径…。、...Read全文
“全速前进是CodeVA的一个会议,致力于通过科学、技术、工程、艺术和数学赋予年轻妇女权力。我们通过将学生与女性榜样联系起来,鼓励他们参与手工工作坊。我们的演讲者将分享他们作为各自行业领导者的经验,强调蒸汽和…的重要性。、...Read全文
由于全州学校关闭,切斯特菲尔德县公立学校正在重新安排幼儿园和幼儿园注册的机会。当取消或放宽与大型集会有关的限制时,面对面的机会都将重新安排。同时,也有机会为未来的幼儿园和幼儿园学生进行网上注册。附呈的新闻稿…、...Read全文
发布于 2020-07-31 16:07:48
我已经创建了两个数组来存储2种不同类型的刮擦数据。pandas.DataFrame()将创建一个数据帧对象,pandas.to_csv()将数据帧对象发送到.csv文件。
这可能不是最有效的代码,但它可以工作。
import requests
from bs4 import BeautifulSoup
import pandas as pd
URL = 'https://mychesterfieldschools.com/mams/news-and-announcements/'
page = requests.get(URL)
soup = BeautifulSoup(page.content, 'html.parser')
results = soup.find_all('div', class_='col-sm-12 col-md-12')
// declaring the 2 arrays for storing your scraped data
text = []
a_tags = []
for results in results:
body = results.find('p')
link = results.find('a')
a = body.text
b = link
print(a) // prints the text (data type string)
print(b) // prints the tag (data type bs4.element.Tag object)
// store the text in text array
text.append(a)
// convert the tags to string and store in a_tags array
a_tags.append(str(b))
// prints the saved arrays
print("text : ", text)
print("tags : ", a_tags)
// creates a pandas dataframe object of the above 2 arrays
df = pd.DataFrame(
{
"Text": text,
"A_tags": a_tags
}
)
// converts to csv
df.to_csv("data.csv", index=False, encoding="utf-8")指向文档的链接:pandas.DataFrame() pandas.to_csv()
输出data.csv文件显示在与您的python脚本相同的目录中。
这是csv在上的外观:

https://stackoverflow.com/questions/63194998
复制相似问题