第二講作業(yè)

import requests

from lxml import etree

import xlwt

all_info_list=[]

def get_info(url):

? ? res = requests.get(url)

? ? html = etree.HTML(res.text)

? ? infos = html.xpath('//div[@class="recommend-article"]/ul/li/div[@class="recmd-right"]')

? ? for info in infos:

? ? ? ? title = info.xpath('a/text()')

? ? ? ? laughs = info.xpath('div/div/span[1]/text()')

? ? ? ? comments = info.xpath('div/div/span[4]/text()')

? ? ? ? id = info.xpath('div/a/span/text()')

? ? ? ? # print(title,laughs,comments,id)

? ? ? ? info_list = [title, laughs, comments, id]

? ? ? ? all_info_list.append(info_list)

if __name__ == '__main__':

? ? book = xlwt.Workbook(encoding='utf-8')

? ? sheet = book.add_sheet('Sheet1')

? ? header = ['題目','好笑數(shù)','評(píng)論數(shù)','作者']

? ? for t in range(len(header)):

? ? ? ? sheet.write(0, t, header[t])

? ? urls = ['https://www.qiushibaike.com/8hr/page/{}/'.format(str(i)) for i in range(1,14)]

? ? for url in urls:

? ? ? ? get_info(url)

? ? i = 1

? ? for list in all_info_list:

? ? ? ? j = 0

? ? ? ? for data in list:

? ? ? ? ? ? sheet.write(i, j, data)

? ? ? ? ? ? j += 1

? ? ? ? i += 1

? ? book.save('C:/Users/madin/Desktop/糗事百科.xls')

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容