'''
3、 糗事百科:xpath
http://www.qiushibaike.com/8hr/page/1
获取列表页每个帖子里的图片、用户昵称、段子内容、点赞次数和评论次数
选做:翻页
写到json文件中
'''
import requests,os,json
from lxml import etree
class Qiubai:
def __call__(self, *args, **kwargs):
self.get_xml(pages)
def get_xml(self,pages):
for page in range(1,pages+1):
base_url = 'https://www.qiushibaike.com/8hr/page/'+str(page)+'/'
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36',
}
params = {}
#网页转码为xml
xml = etree.HTML(requests.get(base_url,headers=headers).text)
self.get_data(xml,page)
#获取终止型号
end_signal = xml.xpath( "/html/body/div[@id='content']/div[@class='content-block clearfix']/div[@class='col1 new-style-col1']/ul[@class='pagination']/li/a/span[@class='next']/text()")
print('————————————————————',base_url)
print('————————————————————',page,end_signal)
if end_signal[0] == '\n更多\n' or int(page) == int(pages):
print('##################################下载