from abc import abstractmethodfrom bs4 import BeautifulSoupimport FileUtilimport HtmlUtilclass Site:def __init__(self):self.file = ''self.page_format = ''self.post_selector = ''@abstractmethoddef phasePage(self, response):pass# 2 to 2321class Site3DM(Site):def __init__(self):super().__init__()self.file = './data/Ex3DM.xlsx'self.page_format = 'https://dl.3dmgame.com/all_all_{}_time/'self.post_selector = 'div[class="listwrap"] > ul > li'def phasePage(self, response):rtn = ''bs = BeautifulSoup(response.content, "lxml")list_post = bs.select('div[class="listwrap"] > ul > li')# Post解析测试# print(list_post[9])file = FileUtil.File3DM(self.file)for i in range(2, len(list_post)):post = list_post[i]title_selector = post.select('div[class="bt"] > a')title_txt = title_selector[0].contents[0]# 获取到标题# print(title_txt)link_url = title_selector[0].attrs['href']# 取得详情链接# print(link_url)img_selector = post.select('img')img_url = img_selector[0].attrs['data-original']# 取得图片链接# print(img_url)# 使用详情链接构造图片名称并下载图片img_txt = link_url.split('/')[-1].split('.')[0]HtmlUtil.download_pic(int(img_txt) % 100, img_txt + '.jpg', img_url)tag_selector = post.select('li[class="li2"] > a')tag_txt = ''for i_tag in tag_selector:# print(i_tag.contents[0], end=',')tag_txt += i_tag.contents[0]tag_txt += ',' # 标签之间逗号分隔# 获取到标签# print(tag_txt)file.setPost([title_txt, link_url, img_txt, tag_txt])file.save()return rtn
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。