爬虫库之 requests_html,json

1. xpinyin 模块

定义:将汉字转换为拼音的的第三方库:xpinyin

安装:

python 复制代码
pip install xpinyin

导包:

python 复制代码
from xpinyin import Pinyin

案例:

1.

python 复制代码
#导入xpinyin模块
from xpinyin import Pinyin

#调用Pinyin类
py=Pinyin()
#get_pinyin 获取中文拼音   默认分隔符  -
print(py.get_pinyin("凤凌"," "))
#tone_marks="marks"  带音标的拼音
print(py.get_pinyin("凤凌"," ",tone_marks="maks"))
print(py.get_initial("凌"))
print(py.get_initials("凤凌"))

2.

python 复制代码
from xpinyin import Pinyin
import requests

py=Pinyin()

headers={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36'
}

word='美女'

#获取请求网址
url=f'https://699pic.com/tupian/{py.get_pinyin(word,"")}.html'
print(url)

with open(f"{word}.html","w",encoding="utf8") as f:
    f.write(requests.get(url,headers=headers).text)
    

3.获取同音汉字

python 复制代码
from xpinyin import Pinyin
import requests

py=Pinyin()

headers={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36'
}

words=["星星","猩猩"]

for word in words:
    #获取中文对应的Id
    pinyin_id_url=f'https://699pic.com/search/getKwInfo?kw={word}'
    response=requests.get(pinyin_id_url,headers=headers)
    print(response.json())  #字符串转换成字典数据
    pinyin_id=response.json().get('data').get('pinyin')

    #获取请求网址
    url=f'https://699pic.com/tupian/{py.get_pinyin(word,"")}.html'
    print(url)


    with open(f"{word}.html","w",encoding="utf8") as f:
        f.write(requests.get(url,headers=headers).text)

2.requests_html模块

2.1 requests_html模块介绍:
  • requests_html是比较新的爬虫库,作者和requests是同一个作者
  • 我们可以在安装的时候看到他安装了lxml,requests,bs4...我们常用的解析和爬取的库都封装在他里面
  • 用法和requests.session实例化的对象用法一模一样,也会自动保存返回信息
  • 相比requests,他多了对于response.html这个属性
  • 注意点:默认是带了有UA的,无需额外添加
2.2 requests_html于requests的区别:
python 复制代码
import requests
import requests_html

session = requests.session()
# requests_html
html_session = requests_html.HTMLSession()

url="http://httpbin.org/get"

response = session.get(url)
print(response.text)


#区别一:提交给服务器的user-Agent伪装成浏览器的user-Agent
html_response=html_session.get(url)
print(html_response.text)

#requests_html 模块中 默认A
html_response=html_session.get(url)
print(html_response.text)

#区别二:为response响应增加了属性名为html对象属性
baidu_url="https://www.baidu.com"
response1=session.get(baidu_url)
html_response1=html_session.get(baidu_url)
html=html_response1.html
print(html,type(html))

3. json 模块

3.1 爬虫中的数据分类:

结构化数据:json,xml等

​ 处理方式:直接转化为python类型

非结构化数据:html

​ 处理方式:正则表达式,xpath,选择器

JSON:全称JavaScript Object Notation,即JavaScript对象标记法,这种数据格式的语法规则是参考JavaScript对象,但是他能被很多语言去使用,是前后端进行交互的一种非常常见的格式,不同语言都有处理JSON格式的数据方法,而在python当初处理JSON则是使用json模块。

**XML:**全称EXtensible Markup Language,即可扩展标记语言,XML是一种标记语言,跟HTML类似,但是XML中的所有标签都没有含义,需要自行去定义标签,XML被设计出来是为了传输数据使用的。在JSON格式出现前,XML也是一种万维网中传输数据使用的最为广泛的一种,但是JSON格式出现后,JSON格式相对于XML的比重反而更多,XML更多是作为配置文件使用。

**HTML:**全称HyperText Markup Language,即超文本标记语言,我们见到的网页数据都是放在各种HTML标签里面的,通过一些解析库,可以从HTML标签获取到我们想要的数据。

3.2 案例:

python 复制代码
import json
#load:从文件中读取json字符串,转换成python字典
print("\n load:从文件中读取json字符串,转换成python字典")
with open("info.json","r",encoding="utf8") as rf:
 data=json.load(rf)
 print(data,type(data))

#loads:将json字符串转换成python字典
print("\n 将json字符串转换成python字典")
json_data = '{"name":"ls","age":28}'
dict_data = json.loads(json_data)
print(dict_data,type(dict_data))

#dump:将python字典转换成json的字符串,并且写入到文件中
print("\n,dump:将python字典转换成json的字符串,并且写入到文件中")
dict_data={"name":"碎花","age":18}
with open("info.json","w",encoding="utf8") as wf:
 json.dump(dict_data,wf,ensure_ascii=False)


#dumps:将python字典转换成json字符串
print("\n dumps:将python字典转换成json字符串")
json_str=json.dumps(dict_data,ensure_ascii=False)
print(json_str,type(json_str))
相关推荐
anOnion1 天前
构建无障碍组件之Radio group pattern
前端·html·交互设计
前端Hardy2 天前
HTML&CSS&JS:丝滑无卡顿的明暗主题切换
javascript·css·html
前端Hardy3 天前
HTML&CSS&JS:基于定位的实时天气卡片
javascript·css·html
前端Hardy3 天前
HTML&CSS:纯CSS实现随机转盘抽奖机——无JS,全靠现代CSS黑科技!
css·html
DeathGhost3 天前
分享URL地址到微信朋友圈没有缩略图?
前端·html
前端Hardy4 天前
HTML&CSS:高颜值产品卡片页面,支持主题切换
css·html
Never_Satisfied4 天前
在HTML & CSS中,nth-child、nth-of-type详解
前端·css·html
cipher4 天前
crawl4ai:AI时代的数据采集利器——从入门到实战
后端·爬虫·python
你怎么知道我是队长4 天前
前端学习---HTML---块元素和行内元素
前端·学习·html
holeer4 天前
【V1.0】Typora 中的 HTML 支持|软件文档自翻译
前端·编辑器·html·typora·web·markdown·文档