The xpath |

The xpath |

Comparable to the intersection

This reptile crawling is popular cities and cities across the country, but due to different rules crawling, it uses the same two rules in 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()

Guess you like

Origin www.cnblogs.com/zx125/p/11409448.html