• xpath的|


    xpath的|

    相当与交集

    本爬虫爬取的是热门城市和全国城市,但是由于爬取的规则不同,所以在同一个xpath中使用了两种规则

    import requests
    from lxml import etree
    
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
    }
    
    def work():
        url='https://www.aqistudy.cn/historydata/'
        res=requests.get(url=url,headers=headers).text
        tree=etree.HTML(res)
        #xpath特殊用法|并集
        a_s=tree.xpath("//div[@class='bottom']/ul/li/a | //div[@class='bottom']/ul/div[2]/li/a")
    
        for i in a_s:
            city_name = i.xpath("./text()")[0]
            print(city_name)
        print(len(a_s))
    
    if __name__ == '__main__':
        work()
    
  • 相关阅读:
    CentOS VPS
    Make 命令教程
    Nginx 学习
    字体对齐问题
    postman curl
    linux命令
    服务器
    操作系统
    nginx-1.15遇到无法加载php文件报404的问题
    thinkphp6.0框架下载与安装
  • 原文地址:https://www.cnblogs.com/zx125/p/11409448.html
Copyright © 2020-2023  润新知