1 ''' 2 @Description: lxml创建html页面 3 @Version: 1.0 4 @Autor: Montoin Yan 5 @Date: 2020-02-05 20:01:17 6 @LastEditors : Montoin Yan 7 @LastEditTime : 2020-02-05 21:06:31 8 ''' 9 from lxml import etree 10 11 root = etree.Element('html') #创建一个父节点 12 body = etree.Element('body') #创建第二个节点 13 14 root.append(body) #以root为父节点将body作为添加到root的字节点中 15 print(etree.tostring(root)) 16 17 div1 = etree.SubElement(body,'div') #设置div块将其设置为body中的子节点 18 div2 = etree.SubElement(body,'div') #同上 19 20 print(etree.tostring(root,pretty_print=True).decode()) #按照root作为父节点将所有内容漂亮的打印出来 21 22 etree.HTML(text) #解析HTML文档,并返回根节点
1 ''' 2 @Description: lxml爬取豆瓣一周口碑榜 3 @Version: 1.0 4 @Autor: Montoin Yan 5 @Date: 2020-02-05 21:16:02 6 @LastEditors : Montoin Yan 7 @LastEditTime : 2020-02-05 21:44:28 8 ''' 9 from lxml import etree 10 import requests 11 import random 12 13 #设置多个请求头,防止被反扒措施进行拦截 14 ua_list = [ 15 "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0", 16 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.29 Safari/537.36", 17 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18362", 18 "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3741.400 QQBrowser/10.5.3863.400" 19 ] 20 #随机pick one 21 ua = random.choice(ua_list) 22 23 url = "https://movie.douban.com/" 24 response = requests.request('GET',url,headers={ 25 'User-agent':ua 26 }) 27 28 content = response.text 29 # print(content) #HTML内容 30 31 #XPath //div[@class='billboard-bd']//tr//a/text() 32 html = etree.HTML(content) #使用XPath解析上下文 33 titles = html.xpath("//div[@class='billboard-bd']//tr//a/text()") #依照XPath要求获取需要的文本 34 for i in titles: 35 print(i)