request+xpath 爬虫案例demo

1. 一个最简单的 request+xpath获取内容

# +----------------------------------------------------------------------
# | User: zq
# | Version: python3.7
# | Time: 2020-03-09 09:33
# +----------------------------------------------------------------------

import requests
from scrapy import Selector

url = "http://www.whzk120.com/index.html"
my_headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
}
res = requests.get(url, headers=my_headers)
#设置编码
res.encoding= "utf8"
html = res.text

sel = Selector(text=html)
# tag = sel.xpath("//*[@class='indexPart1']/ul/li[1]/a/h2/text()").extract()[0]
tag = sel.xpath("//*[@class='li_01']/a/h2/text()").extract()[0]
pass

 

你可能感兴趣的:(python)