Python爬虫requests包和xpath包总结


import requests

# 这里的headers就是我们上图框中的headers
request_headers = {     'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': "zh-CN,zh;q=0.9",
    'Cache-Control': 'no-cache',
    'cookie': '',
    'Pragma': 'no-cache',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36 }'
    }#上图中的url
url = "https://www.douban.com/note/704821854/"
# 上图中的请求方法(get)
z = requests.get(url, headers=request_headers)
print(z.text)


from lxml import etree

sample1 = """
  
    My page
  
  
    

Welcome to my page

This is the first paragraph.

""" s1 = etree.HTML(sample1) # 获取标题(两种方法都可以,第一种为绝对地址,第二种为相对地址) print(s1) print(s1.xpath('//title/text()')) #相对地址 print(s1.xpath('/html/head/title/text()')) #绝对地址 print(s1.xpath('//h2/a/@src')) print(s1.xpath('//@href')) print(s1.xpath('//text()')) #获取所有的文本 print(s1.xpath('//comment()')) #获取所有的注释 sample2 = """ """ s2 = etree.HTML(sample2) print(s2.xpath('//li/text()')) print(s2.xpath('//li[1]/text()')) print(s2.xpath('//li[a or h2]/text()')) print(s2.xpath('//a/text()|//h2/text()')) sample3 = """ """ s3 = etree.HTML(sample3) print(s3.xpath('//li/a[@href = "https://scrapy.org"]/text()')) print(s3.xpath('//li[@abc="abc"]/text()')) from lxml import etree sample4 = u""" My page

Welcome to my page

This is the first paragraph.

编程语言python testjavascript C#JAVA

a

b

c

d

e

f

""" s4 = etree.HTML(sample4) print(s4.xpath('string(//p[@class = "test"])')) #标签中的标签文本 print(s4.xpath('//p[contains(@class, "content")]/text()')) #包含

requests包对文件进行抓取,xpath包对html文件进行提取操作

string和text不能同时使用,string只能获取单个节点

参考资料:https://zhuanlan.zhihu.com/p/25572729

你可能感兴趣的:(nlp)