目前我将从世界各地的教区收集数据。
我的方法适用于 bs4 和 pandas。我目前正在研究抓取逻辑。
import requests from bs4 import BeautifulSoup import pandas as pd url = "http://www.catholic-hierarchy.org/" # Send a GET request to the website response = requests.get(url) #my approach to parse the HTML content of the page soup = BeautifulSoup(response.text, 'html.parser') # Find the relevant elements containing diocese information diocese_elements = soup.find_all("div", class_="diocesan") # Initialize empty lists to store data dioceses = [] addresses = [] # Extract now data from each diocese element for diocese_element in diocese_elements: # Example: Extracting diocese name diocese_name = diocese_element.find("a").text.strip() dioceses.append(diocese_name) # Example: Extracting address address = diocese_element.find("div", class_="address").text.strip() addresses.append(address) # to save the whole data we create a DataFrame using pandas data = {'Diocese': dioceses, 'Address': addresses} df = pd.DataFrame(data) # Display the DataFrame print(df)
目前我的 pycharm 上发现了一些奇怪的东西。 我尝试找到一种使用pandas 方法收集全部数据的方法。
正确答案
这个示例可以帮助您入门 - 它将解析所有教区页面以获取教区名称 + url,并将其存储到 panda 的 dataframe 中。
然后您可以迭代这些 url 并获取所需的更多信息。
import pandas as pd import requests from bs4 import beautifulsoup chars = "abcdefghijklmnopqrstuvwxyz" url = "http://www.catholic-hierarchy.org/diocese/la{char}.html" all_data = [] for char in chars: u = url.format(char=char) while true: print(f"parsing {u}") soup = beautifulsoup(requests.get(u).content, "html.parser") for a in soup.select("li a[href^=d]"): all_data.append( { "name": a.text, "url": "http://www.catholic-hierarchy.org/diocese/" + a["href"], } ) next_page = soup.select_one('a:has(img[alt="[next page]"])') if not next_page: break u = "http://www.catholic-hierarchy.org/diocese/" + next_page["href"] df = pd.dataframe(all_data).drop_duplicates() print(df.head(10))
打印:
... Parsing http://www.catholic-hierarchy.org/diocese/lax.html Parsing http://www.catholic-hierarchy.org/diocese/lay.html Parsing http://www.catholic-hierarchy.org/diocese/laz.html Name URL 0 Holy See http://www.catholic-hierarchy.org/diocese/droma.html 1 Diocese of Rome http://www.catholic-hierarchy.org/diocese/droma.html 2 Aachen http://www.catholic-hierarchy.org/diocese/da549.html 3 Aachen http://www.catholic-hierarchy.org/diocese/daach.html 4 Aarhus (Århus) http://www.catholic-hierarchy.org/diocese/da566.html 5 Aba http://www.catholic-hierarchy.org/diocese/dabaa.html 6 Abaetetuba http://www.catholic-hierarchy.org/diocese/dabae.html 8 Abakaliki http://www.catholic-hierarchy.org/diocese/dabak.html 9 Abancay http://www.catholic-hierarchy.org/diocese/daban.html 10 Abaradira http://www.catholic-hierarchy.org/diocese/d2a01.html
以上是美丽的汤解析许多条目的列表并保存在数据框中的详细内容。更多信息请关注PHP中文网其他相关文章!

Python在自动化、脚本编写和任务管理中表现出色。1)自动化:通过标准库如os、shutil实现文件备份。2)脚本编写:使用psutil库监控系统资源。3)任务管理:利用schedule库调度任务。Python的易用性和丰富库支持使其在这些领域中成为首选工具。

要在有限的时间内最大化学习Python的效率,可以使用Python的datetime、time和schedule模块。1.datetime模块用于记录和规划学习时间。2.time模块帮助设置学习和休息时间。3.schedule模块自动化安排每周学习任务。

Python在游戏和GUI开发中表现出色。1)游戏开发使用Pygame,提供绘图、音频等功能,适合创建2D游戏。2)GUI开发可选择Tkinter或PyQt,Tkinter简单易用,PyQt功能丰富,适合专业开发。

Python适合数据科学、Web开发和自动化任务,而C 适用于系统编程、游戏开发和嵌入式系统。 Python以简洁和强大的生态系统着称,C 则以高性能和底层控制能力闻名。

2小时内可以学会Python的基本编程概念和技能。1.学习变量和数据类型,2.掌握控制流(条件语句和循环),3.理解函数的定义和使用,4.通过简单示例和代码片段快速上手Python编程。

Python在web开发、数据科学、机器学习、自动化和脚本编写等领域有广泛应用。1)在web开发中,Django和Flask框架简化了开发过程。2)数据科学和机器学习领域,NumPy、Pandas、Scikit-learn和TensorFlow库提供了强大支持。3)自动化和脚本编写方面,Python适用于自动化测试和系统管理等任务。

两小时内可以学到Python的基础知识。1.学习变量和数据类型,2.掌握控制结构如if语句和循环,3.了解函数的定义和使用。这些将帮助你开始编写简单的Python程序。

如何在10小时内教计算机小白编程基础?如果你只有10个小时来教计算机小白一些编程知识,你会选择教些什么�...


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

Atom编辑器mac版下载
最流行的的开源编辑器

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

WebStorm Mac版
好用的JavaScript开发工具