python爬虫练习1

402次阅读
没有评论
python爬虫练习1


目录索引:

​​python爬虫练习6:今日头条搜索图集​​​​python爬虫练习5:博客阅读量助手​​python爬虫练习4:刷浏览量
​​python爬虫练习3:豆瓣电影推荐页电影详情​​​​python爬虫练习2:名言作者​​​​python爬虫练习1:颜色颜色值​​


难度


目标网站

​​http://html-color-codes.info/color-names/​​

用到库

requests,beautifulsoup4,lxml

系统说明

python3.6.1 64位

目标

获取颜色与颜色值等


import requests
from bs4 import BeautifulSoup
hdrs={‘User-Agent’:‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36’}
url=“http://html-color-codes.info/color-names/”
r=requests.get(url=url,headers=hdrs)
soup=BeautifulSoup(r.content, “lxml”)

#print(soup.prettify())

print(soup.title.string)
trs=soup.find_all(‘tr’)
for tr in trs:
style=tr.get(‘style’)
tds=tr.find_all(‘td’)
td=[x for x in tds]
name=td[1].text
hex=td[2].text
print(‘颜色:%-20s值:%-20s样式:%-20s’%(name,hex,style))

结果:

D:softInstallPython36python.exe E:/SourceCode/PythonWP/pachong/index.py
Color Names
颜色:IndianRed 值:CD5C5C 样式:background-color:indianred;
颜色:LightCoral 值:F08080 样式:background-color:lightcoral;
颜色:Salmon 值:FA8072 样式:background-color:salmon;
颜色:DarkSalmon 值:E9967A 样式:background-color:darksalmon;
颜色:LightSalmon 值:FFA07A 样式:background-color:lightsalmon;
颜色:Crimson 值:DC143C 样式:background-color:crimson;
颜色:Red 值:FF0000 样式:background-color:red;
颜色:FireBrick 值:B22222 样式:background-color:fireBrick;
颜色:DarkRed 值:8B0000 样式:background-color:darkred;


神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:Python教程2022-11-01发表,共计1264字。
新手QQ群:570568346,欢迎进群讨论 Python51学习