1.使用requests和 xpath模块爬取网页内容
1.from fake_useragent import UserAgent
动态设置消息头
使用下面模块 爬取网页内容
网页解析使用xpath
xpath 语法 最简单使用谷歌浏览器 定位标签 copy xpath
#!/usr/bin/env python
#-*- coding:utf-8 -*-
"""
@author: zhengxianjun
@contact: 1596492090@qq.com
@datetime:2020/6/5 22:25
@software: PyCharm
"""
if __name__ == '__main__':
"""
使用下面模块 爬取网页内容
网页解析使用xpath
xpath 语法 最简单使用谷歌浏览器 定位标签 copy xpath
"""
import requests
import re
from lxml import etree
from fake_useragent import UserAgent
# 消息头 随机选取
headers = {
'User-Agent': UserAgent().random
}
url = ''
# 1.连接请求 解析HTML
resp = requests.get(url=url, headers=headers).content.decode('utf8')
html = etree.HTML(resp)
# 11.如果是Ajax请求 直接解析json数据 这个比较好处理
# resp = reques