Python 30 天 :第 22 天 -- 网页抓取

<< 第 21 天 || 第 23 天 >>

第 22 天

Python 网页爬取

什么是网页爬取

互联网充满了可用于不同目的的大量数据。要收集这些数据,我们需要知道如何从网站上抓取数据。

Web 抓取是从网站提取和收集数据并将其存储在本地计算机或数据库中的过程。

在本节中,我们将使用 beautifulsoup 和 requests 包来抓取数据。我们使用的包版本是 beautifulsoup 4。

要开始抓取网站,您需要请求beautifoulSoup4和一个网站

pip install requests
pip install beautifulsoup4

要从网站上抓取数据,需要对 HTML 标签和 CSS 选择器有基本的了解。我们使用 HTML 标签、类或/和 id 从网站定位内容。让我们导入 requests 和 BeautifulSoup 模块

import requests
from bs4 import BeautifulSoup

让我们为要抓取的网站声明 url 变量。


import requests
from bs4 import BeautifulSoup
url = 'https://archive.ics.uci.edu/ml/datasets.php'

# Lets use the requests get method to fetch the data from url

response = requests.get(url)
# lets check the status
status = response.status_code
print(status) # 200 means the fetching was successful
200

使用 beautifulSoup 解析页面内容

import requests
from bs4 import BeautifulSoup
url = 'https://archive.ics.uci.edu/ml/datasets.php'

response = requests.get(url)
content = response.content # we get all the content from the website
soup = BeautifulSoup(content, 'html.parser') # beautiful soup will give a chance to parse
print(soup.title) # <title>UCI Machine Learning Repository: Data Sets</title>
print(soup.title.get_text()) # UCI Machine Learning Repository: Data Sets
print(soup.body) # gives the whole page on the website
print(response.status_code)

tables = soup.find_all('table', {'cellpadding':'3'})
# We are targeting the table with cellpadding attribute with the value of 3
# We can select using id, class or HTML tag , for more information check the beautifulsoup doc
table = tables[0] # the result is a list, we are taking out data from it
for td in table.find('tr').find_all('td'):
    print(td.text)

如果运行此代码,您会看到提取已完成一半。您可以继续这样做,因为它是练习 1 的一部分。作为参考,请查看beautifulsoup 文档

🌕你是如此特别,你每天都在进步。你只有八天的时间才能走向伟大。现在为你的大脑和肌肉做一些练习。

练习: 第 22 天

  1. 抓取以下网站并将数据存储为 json 文件(url = ' BU Facts & Stats | Office of the President ')。
  2. 提取此 url ( https://archive.ics.uci.edu/ml/datasets.php )中的表并将其更改为 json 文件
  3. 抓取总统表并将数据存储为 json(https://en.wikipedia.org/wiki/List_of_presidents_of_the_United_States)。该表不是很结构化,报废可能需要很长时间。

 🎉恭喜!🎉

<< 第 21 天 || 第 23 天 >>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

舍不得,放不下

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值