def getproxyfromxici():
url = 'https://www.xicidaili.com/nn/1'
header = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36'}
respones = requests.get(url,headers = header )
srcfromhtml= respones.text
selector = etree.HTML(srcfromhtml)
#print(etree.tostring(selector).decode('utf-8'))
srcofxpath = '//*[@id="ip_list"]/tbody/tr[2]/td[2]' #原标签有个tbody就不能用,不知道为什么
srcofxpathchangge = '//*[@id="ip_list"]//tr[2]/td[2]/text()' #去掉tbody可以用
srcofxpath2 = '//*[@id="ip_list"]/tr/td[2]/text()' #不加//也能用
titlelist = [selector.xpath(srcofxpath2.replace('[2]','['+str(i)+']')) for i in [6,2,3]]
proxydict = [{x:y+':'+z}for x,y,z in zip(titlelist[0],titlelist[1],titlelist[2])]
for proxy in proxydict:
str(proxy)
lxml标签提取案例及说明
最新推荐文章于 2024-02-29 17:24:02 发布