爬虫库之 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))
相关推荐
深蓝电商API5 分钟前
滑块验证码破解思路与常见绕过方法
爬虫·python
sensen_kiss19 分钟前
INT303 Coursework1 爬取影视网站数据(如何爬虫网站数据)
爬虫·python·学习
小小张说故事3 小时前
BeautifulSoup:Python网页解析的优雅利器
后端·爬虫·python
一晌小贪欢4 小时前
Python 爬虫进阶:如何利用反射机制破解常见反爬策略
开发语言·爬虫·python·python爬虫·数据爬虫·爬虫python
NuageL5 小时前
原始Json字符串转化为Java对象列表/把中文键名变成英文键名
java·spring boot·json
x-cmd5 小时前
[x-cmd] jsoup 1.22.1 版本发布,引入 re2j 引擎,让 HTML 解析更安全高效
前端·安全·html·x-cmd·jsoup
深蓝电商API5 小时前
爬虫请求频率控制与模拟人类行为
爬虫
喵手6 小时前
Python爬虫实战:知识挖掘机 - 知乎问答与专栏文章的深度分页采集系统(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集知乎问答与专栏文章·采集知乎数据·采集知乎数据存储sqlite
摇滚侠6 小时前
解释一下 JSON 文件中,能不能写注释,postman 中,定义 json 格式的接口参数,能写注释吗
json
禹凕6 小时前
Python编程——进阶知识(多线程)
开发语言·爬虫·python