import requests
from lxml import etree
def text_create(name, msg):
desktop_path = "D:\\test\\" # 新創建的txt文件的存放路徑
full_path = desktop_path + str(name) + '.txt' # 也可以創建一個.doc的word文檔
file = open(full_path, 'w')
file.write(msg) #msg也就是下面的Hello world!
def title(headers,url):
response = requests.get(url, headers=headers)
text = response.text
html = etree.HTML(text)
# 獲取標題
titles = html.xpath("//div[@class='course-details-title-cont-text']//ul//li//h2/text()")[0].replace('\t','')
# 獲取主講人
pepoe = html.xpath("//div[@class='course-details-title-cont-text']//ul//li//p//span[1]//text()")
class_test = html.xpath("//div[@class='course-details-view-list-introduce-cont']//p[@class='middleColor']/text()")[0]
test_txt="標題\n"+str(titles)+"主講人\n"+str(pepoe[0])+"章節數\n"+str(pepoe[1])+"課時\n"+str(pepoe[2])+"學習人數\n"+str(pepoe[3])+"課程簡介\n"+str(class_test)
print(pepoe)
text_create(titles, test_txt)
#
# if str(pepoe[1])=="0":
# print("文件異常過濾" +titles)
# return 1;
#
# else:
# text_create(titles,test_txt)
# return 0;
if __name__ == '__main__':
numberss=0
for i in range(0,5):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36'
}
url = "https://www.51moot.net/main/course?search_id=0&is_free=-1&page_index="+str(i)
response = requests.get(url, headers=headers)
text = response.text
html = etree.HTML(text)
new_url = html.xpath("//div[@class='course-details-cont-view']//ul//li//a//@href")
for i in range(len(new_url)):
title(headers, "https://www.51moot.net" + new_url[i])
# temp=
# numberss=numberss+int(temp)
# print(numberss)