【python】爬取元素周期表的元素信息

假期的第一个小任务
是python以及爬虫的初体验?
https://www.webelements.com/hydrogen/
先尝试了一下这里
在这里插入图片描述

>>> import requests
>>> import bs4
>>> from bs4 import BeautifulSoup
>>> r = requests.get("https://www.webelements.com/hydrogen/")
>>> r.status_code
200
>>> demo = r.text
>>> soup = BeautifulSoup(demo,"html.parser")
>>> for ul in soup.find_all('ul','ul_facts_table'):
	for lis in ul.contents:
		if (type(lis) == bs4.element.Tag):
			for tag in lis.contents:
				print(tag.string)

结果:
在这里插入图片描述
在这里插入图片描述
后续处理下次再说8~

7.23更新
完成~
完整代码如下

import requests
import bs4
from bs4 import BeautifulSoup
import xlwt

homepage = "https://www.webelements.com"
r = requests.get("https://www.webelements.com"
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用 Python 爬取网页上的元素颜色信息,可以使用以下步骤: 1. 首先,需要安装 `requests` 和 `beautifulsoup4` 这两个 Python 库,可以使用以下命令来安装: ``` pip install requests beautifulsoup4 ``` 2. 使用 `requests` 库向网页发送请求,并获取网页的 HTML 内容。例如,以下代码将请求百度首页,并将其 HTML 内容保存到变量 `html` 中: ``` import requests url = 'https://www.baidu.com' response = requests.get(url) html = response.text ``` 3. 使用 `beautifulsoup4` 库解析 HTML 内容,并查找需要的元素。例如,以下代码将查找百度首页的搜索框元素,并将其保存到变量 `input_elem` 中: ``` from bs4 import BeautifulSoup soup = BeautifulSoup(html, 'html.parser') input_elem = soup.find('input', {'id': 'kw'}) ``` 4. 获取元素的颜色信息元素的颜色信息通常保存在其 `style` 属性中。例如,以下代码将获取搜索框元素的背景颜色信息: ``` bg_color = input_elem.get('style').split(':')[1] ``` 注意,这里假设搜索框元素的 `style` 属性的第二个分号后面是背景颜色信息。如果实际情况不同,需要根据具体情况进行调整。 完整代码示例: ``` import requests from bs4 import BeautifulSoup url = 'https://www.baidu.com' response = requests.get(url) html = response.text soup = BeautifulSoup(html, 'html.parser') input_elem = soup.find('input', {'id': 'kw'}) bg_color = input_elem.get('style').split(':')[1] print('Search box background color:', bg_color) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值