Python:这是一个朴实无华的爬虫教程,就是有点养眼,成长路线图
warning:
这篇文章距离上次修改已过202天,其中的内容可能已经有所变动。
import requests
from bs4 import BeautifulSoup
# 爬取成长路线图的函数
def crawl_evolution_chain(url):
response = requests.get(url)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
chain_list = soup.find('tbody', class_='evolution-chain').find_all('tr')
for chain in chain_list:
cells = chain.find_all('td')
for cell in cells:
pokemon_name = cell.find('a', class_='ent-name')
if pokemon_name:
print(pokemon_name.text)
else:
print("Error:", response.status_code)
# 成长路线图的URL
url = 'https://pokemondb.net/pokedex/all'
# 调用函数爬取成长路线图
crawl_evolution_chain(url)
这段代码使用了requests库来发送HTTP请求,以及BeautifulSoup库来解析HTML页面。它定义了一个函数crawl_evolution_chain
,该函数接受一个URL作为参数,发送请求,并解析页面以提取每个Pokemon条目。然后它遍历每个条目,打印出Pokemon的名字。这个例子展示了如何使用Python爬取简单的表格数据,并且是学习网络爬虫的一个很好的起点。
评论已关闭