lmxl的xpath提取脚本之间的title以及url

  运用lmxl的xpath提取脚本之间的title以及url:

import urllib.request
import lxml
import lxml.etree
import re
def  makeurllist(url):
    headers={"User-Agent":"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0);"}
    request = urllib.request.Request(url, headers=headers)  # 请求,修改,模拟http.
    data = urllib.request.urlopen(request).read()  # 打开请求,抓取数据
    mytree = lxml.etree.HTML(data)
    mylist=mytree.xpath("//*[@class=\"dxypage clearfix\"]//text()")
    lastmystr=mylist[0]
    restr="页次:1/(\d+) 每页"
    regex=re.compile(restr,re.IGNORECASE)
    mylastlist=regex.findall(lastmystr)
    numbers=eval(mylastlist[0])  #整数
    urllist=[]
    for i in range(1,numbers+1):
        urllist.append("https://www.jb51.net/list/list_97_"+str(i)+".htm")

    #print(urllist)
    return urllist
def gettitlefromurl(url):
    lastlist=[]     #[(title,url),(title,url),(title,url),(title,url)]
    headers = {"User-Agent": "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0);"}
    request = urllib.request.Request(url, headers=headers)  # 请求,修改,模拟http.
    data = urllib.request.urlopen(request).read()  # 打开请求,抓取数据
    mytree = lxml.etree.HTML(data)
    urllist=mytree.xpath("//*[@class=\"artlist clearfix\"]/dl/dt/a/@href")  #* 匹配任何元素节点    //*[@class=\"artlist clearfix\"] 表示从当前节点下降的所有元素节点
    #print(urllist)
    titlelist = mytree.xpath("//*[@class=\"artlist clearfix\"]/dl/dt/a/@title")
    #print(titlelist)
    for list1 in urllist:
        for list2 in titlelist:
            lastlist.append((list2,list1))
    #print(lastlist)
    return lastlist

url="https://www.jb51.net/list/list_97_1.htm"
#makeurllist("https://www.jb51.net/list/list_97_1.htm")
#gettitlefromurl("https://www.jb51.net/list/list_97_1.htm")
for allurl in makeurllist(url):
    gettitlefromurl(allurl)
    print(gettitlefromurl(allurl))  #返回列表元组[(title,url),(title,url),(title,url),(title,url)]

 

posted on 2020-03-10 10:21  共感的艺术  阅读(780)  评论(0编辑  收藏  举报