import requests
import re
import os
from parsel import Selector
from fake_useragent import UserAgent
import csv
import time, random
import json
class WisdomBagSearch(object):
def __init__(self):
self.MainUrl = "https://chiebukuro.yahoo.co.jp"
self.MaxNumPage = 2
def writeHtml(self, url, html):
with open(url, 'w', encoding='utf-8') as f:
f.write(str(html))
def GetHtml(self, Url):
header = {
"user-agent": UserAgent().random
}
try:
response = requests.get(Url, headers=header)
return response.text
except Exception as e:
print(e)
return None
def GetCategory(self, firstLayerUrl):
'''
:param firstLayerUrl: 第一层地址
:return: 返回所有类别的href值
'''
totleHtml = self.GetHtml(firstLayerUrl)
totleSelector = Selector(text=totleHtml)
totleTiems = totleSelector.css(
'.ClapLv2CategoryList_Chie-CategoryList__Category2Wrapper__llQoL a::attr(href)').getall()
return totleTiems
def preHanle_categoryHrefS(self, categoryHrefS):
'''
:param categoryHrefS: 所有类别的href
:return: 所有类别可以直接访问的的地址
'''
categoryUrls = []
for categoryHref in categoryHrefS:
categoryUrl = self.MainUrl + categoryHref + "?flg=1"
categoryUrls.append(categoryUrl)
return categoryUrls
def GetQuestionsUrls(self, categoryUrl):
'''
:param categoryUrl: 类别的地址,SecondLayer
:return: MaxNumPage页内的所有问题的Url
'''
questionUrls = []
for i in range(1, self.MaxNumPage + 1):
try:
NewcategoryUrl = categoryUrl + "&page=" + str(i)
NewcategoryHtml = self.GetHtml(NewcategoryUrl)
categorySelector = Selector(text=NewcategoryHtml)
categoryItems = categorySelector.css('.ClapLv3List_Chie-List__ListItem__y_P8W a::attr(href)').getall()
questionUrls.append(categoryItems)
except Exception as e:
print(e)
return [i for j in questionUrls for i in j]
def AnswersNormalization(self, anotherAnswerItems):
'''
:param anotherAnswerItems: 其他回答的文本
:return: 格式化,删除换行,超文本链接
'''
try:
if len(anotherAnswerItems) > 0:
anotherAnswerItems = list(anotherAnswerItems)
for i in range(len(anotherAnswerItems)):
anotherAnswerItems[i] = re.sub("<.*?>", '', anotherAnswerItems[i])
anotherAnswerItems[i] = anotherAnswerItems[i].replace('\n', '')
anotherAnswerItems[i] = anotherAnswerItems[i].replace('\r', '')
return anotherAnswerItems
except Exception as e:
print(type(e), e)
return None
def GetUserInfo(self, userInfoUrl):
'''
:param userInfoUrl: yahoo用户地址
:return: 用户登陆号,名字
'''
if type(userInfoUrl) != type('2') or len(userInfoUrl) < 35 or str(
userInfoUrl[:35]) != 'https://chiebukuro.yahoo.co.jp/user':
AnswerItems = ['None', 'D非公開さん']
return AnswerItems
userHtml = self.GetHtml(userInfoUrl)
AnswerSelector = Selector(text=userHtml)
AnswerItems = AnswerSelector.css('.ClapLv2MyProfile_Chie-MyProfile__ContentItem__DfPaV *::text').getall()
return [AnswerItems[1], AnswerItems[2]]
def GetUserInfo2(self, answerHtml):
answerUserInfoSelector = Selector(text=answerHtml)
answerUserUrl = answerUserInfoSelector.css(
'.ClapLv2AnswerItem_Chie-AnswerItem__ItemHead__Mvlc0 a::attr(href)').get()
loginnumber = os.path.basename(answerUserUrl)
userName = answerUserInfoSelector.css('.ClapLv1UserInfo_Chie-UserInfo__UserName__1bJYU *::text').get()
print(loginnumber)
print(userName)
return [loginnumber, userName]
def CheckQuestionStandard(self, questionInfomation):
return True
def GetAnswerInfo(self, answerHtml):
answer = {}
answerTextSelector = Selector(text=answerHtml)
answerText = answerTextSelector.css('.ClapLv1TextBlock_Chie-TextBlock__3X4V5 h2::text').getall()
NewanswerText = ""
for text in answerText:
NewanswerText += text
NewanswerText = NewanswerText.replace('\n', '')
NewanswerText = NewanswerText.replace('\r', '')
answerTimeSelector = Selector(text=answerHtml)
answerTime = answerTimeSelector.css('.ClapLv1UserInfo_Chie-UserInfo__Date__2F1LF *::text').get()
answerUserInfoSelector = Selector(text=answerHtml)
answerUserUrl = answerUserInfoSelector.css(
'.ClapLv2AnswerItem_Chie-AnswerItem__ItemHead__Mvlc0 a::attr(href)').get()
answerUserInfo = self.GetUserInfo2(answerUserUrl)
answerApprovelSelector = Selector(text=answerHtml)
answerApprove = answerApprovelSelector.css(
'.ClapLv1ReactionCounter_Chie-ReactionCounter__Text__1yosc *::text').get()
answerReplaySelector = Selector(text=answerHtml)
answerReplayItems = answerReplaySelector.css(".ClapLv3ReplyList_Chie-ReplyList__Item__33upu").getall()
replys = []
self.GetUserInfo2(answerHtml)
if len(answerReplayItems) != 0:
for answerReplay in answerReplayItems:
reply = {}
self.GetUserInfo2(answerReplay)
answerReplyTextSelector = Selector(text=answerReplay)
answerReplyText = answerReplyTextSelector.css(
'.ClapLv1TextBlock_Chie-TextBlock__3X4V5 *::text').getall()
NewanswerReplyText = ""
for text in answerReplyText:
NewanswerReplyText += text
NewanswerReplyText = NewanswerReplyText.replace('\n', '')
NewanswerReplyText = NewanswerReplyText.replace('\r', '')
answerReplyTimeSelector = Selector(text=answerHtml)
answerReplyTime = answerReplyTimeSelector.css(
'.ClapLv1UserInfo_Chie-UserInfo__DateSmall__3erUK *::text').get()
answerReplyUserInfoSelector = Selector(text=answerHtml)
answerUserReplyUrl = answerReplyUserInfoSelector.css(
'.ClapLv2ReplyItem_Chie-ReplyItem__ItemHead__HrG5K a::attr(href)').get()
answerUserReplyInfo = self.GetUserInfo2(answerUserReplyUrl)
answerUserReplyInfoExp = {'Loginnumber': answerUserReplyInfo[0],
'UserName': answerUserReplyInfo[1]}
reply['ReplyText'] = NewanswerReplyText
reply['ReplyTime'] = answerReplyTime
reply['ReplyUserInfo'] = answerUserReplyInfoExp
replys.append(reply)
answer['AnswerText'] = NewanswerText
answer['AnswerTime'] = answerTime
answer['AnswerUserInfo'] = {'Loginnumber': answerUserInfo[0],
'UserName': answerUserInfo[1]}
answer['AnswerApprove'] = answerApprove
answer['AnswerReply'] = replys
if answerApprove == None:
answer['AnswerApprove'] = 0
return answer
def GetAnswersInfo(self, answerUrl):
'''
:param answerUrl: 问题的地址
:return: 所有回答的内容,时间,用户账户信息,点赞数
'''
answers = []
answerHtml = self.GetHtml(answerUrl)
answerTextSelector = Selector(text=answerHtml)
answerHtml = answerTextSelector.css('#ba .ClapLv3BestAnswer_Chie-BestAnswer__1kJ7F').get()
baAnswerInfo = self.GetAnswerInfo(answerHtml)
try:
baAnswerInfo['AnswerText'] = baAnswerInfo['AnswerText'][7:]
except Exception as e:
print(e)
answers.append(baAnswerInfo)
pageReply = (int(self.GetOtherAnswerNumber(answerUrl)) - 1) // 5 + 1
for i in range(1, pageReply + 1):
NewPageReplyUrl = answerUrl + '?sort=1&page=' + str(i)
questionHtml = self.GetHtml(NewPageReplyUrl)
AnswerSelector = Selector(text=questionHtml)
AnswerItems = AnswerSelector.css('#ans .ClapLv3AnswerList_Chie-AnswerList__Item__2PxD4').getall()
for answerHtml in AnswerItems:
answerInfo = self.GetAnswerInfo(answerHtml)
answers.append(answerInfo)
return answers
def GetQuestionMessage(self, questionUrl):
questionInformation = []
try:
questionHtml = self.GetHtml(questionUrl)
self.writeHtml('./test12.txt', questionHtml)
authorNameSelector = Selector(text=questionHtml)
authorUrl = authorNameSelector.css(
'#que .ClapLv2QuestionItem_Chie-QuestionItem__Head__1ZglB a::attr(href)').get()
authorInfo = self.GetUserInfo(authorUrl)
authorInfo = {'Loginnumber': authorInfo[0], 'UserName': authorInfo[1]}
authorTimeSelector = Selector(text=questionHtml)
authorTimeItems = authorTimeSelector.re('itemprop="dateCreated".*?>(.*?)')
authorQuestionSelector = Selector(text=questionHtml)
authorQuestionItems = authorQuestionSelector.css(
'#que .ClapLv1TextBlock_Chie-TextBlock__3X4V5 *::text').getall()
queText = authorQuestionItems[0]
queGlan = authorQuestionItems[-2]
authorQuestionlabelSelector = Selector(text=questionHtml)
authorQuestionLabelItems = authorQuestionlabelSelector.css(
'#que .ClapLv2QuestionItem_Chie-QuestionItem__SubAnchor__2Pv8w *::text').getall()
authorThankSelector = Selector(text=questionHtml)
authorThankItems = authorThankSelector.css(
'.ClapLv3BestAnswer_Chie-BestAnswer__Thanks__1ASeS *::text').getall()
awardMoneyQuestionSelector = Selector(text=questionHtml)
awardMoneyQuestionItems = awardMoneyQuestionSelector.css(
'#que .ClapLv2QuestionItem_Chie-QuestionItem__SubChieCoin__2akxj *::text').getall()
awardMoney = 0
if len(awardMoneyQuestionItems) == 2:
awardMoney = awardMoneyQuestionItems[1]
baAnswerSelector = Selector(text=questionHtml)
baAnswerSelectorItems = baAnswerSelector.css('#ba .ClapLv1TextBlock_Chie-TextBlock__3X4V5 *::text').getall()
texts = []
texts.append(queText)
if 0 < len(baAnswerSelectorItems) <= 2:
bestAnswer = baAnswerSelectorItems[1]
texts.append(bestAnswer)
else:
bestAnswer = baAnswerSelectorItems[1]
thanksAnswer = baAnswerSelectorItems[-3]
thanksAnswerTime = baAnswerSelectorItems[-1]
authorTimeItems.append(thanksAnswerTime)
texts.append(bestAnswer)
texts.append(thanksAnswer)
anotherAnswerSelector = Selector(text=questionHtml)
anotherAnswerItems = anotherAnswerSelector.css('#ans .ClapLv1TextBlock_Chie-TextBlock__3X4V5').getall()
for anotherAnswer in anotherAnswerItems:
texts.append(anotherAnswer)
questionInformation.append(texts[0])
questionInformation.append(authorQuestionLabelItems)
questionInformation.append(authorTimeItems[0])
questionInformation.append(authorInfo)
questionInformation.append(questionUrl)
questionInformation.append(awardMoney)
answers = self.GetAnswersInfo(questionUrl)
if len(authorThankItems) > 4:
ThansInfo = {}
ThansInfo['AnswerText'] = authorThankItems[2].replace('\n', '')
ThansInfo['AnswerTime'] = authorThankItems[4]
ThansInfo['AnswerUserInfo'] = authorInfo
ThansInfo['AnswerApprove'] = 0
answers.append(ThansInfo)
questionInformation.append(answers)
return questionInformation
except Exception as e:
return []
def WriteInCsv(self, csvSaveUrl, questionInformation):
dataList = []
for question, user, time, text in zip(*questionInformation):
if user == '' or time == '' or text == '':
continue
simpleInfomation = {}
simpleInfomation['Question'] = question
simpleInfomation['Time'] = time
simpleInfomation['UserName'] = user
simpleInfomation['Text'] = text
dataList.append(simpleInfomation)
fieldnames = ['Question', 'Time', 'UserName', 'Text']
with open(csvSaveUrl, 'a+', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writerows(dataList)
def WriteJson(self, jsonUrl, questionInformation):
fieldnames = ['QuestionText', 'QuestionLabel', 'QuestionTime', 'QuestionerInformation', 'QuestionUrl',
'OfferReward', 'Answers']
messageItem = {}
try:
if len(fieldnames) == len(questionInformation):
for i in range(len(fieldnames)):
messageItem[fieldnames[i]] = questionInformation[i]
json.dump(messageItem, open(jsonUrl, 'a+', encoding='utf-8'), indent=2, ensure_ascii=False)
except:
pass
def GetCateName(self, categoryUrl):
categoryHtml = self.GetHtml(categoryUrl)
cateNameSelector = Selector(text=categoryHtml)
cateName = cateNameSelector.css('.ClapLv2Title_Chie-Title__TextWrapper__1ccaf h1::text').get()
return cateName
def GetOtherAnswerNumber(self, answerUrl):
'''
:param answerUrl: 问题网页的资源定位符
:return:该问题下的其他回复数
'''
answerHtml = self.GetHtml(answerUrl)
answerAnswerSelector = Selector(text=answerHtml)
answerNumber = answerAnswerSelector.css(
'.ClapLv2QuestionItem_Chie-QuestionItem__AnswerNumber__3_0RR *::text').get()
if answerNumber == None:
answerNumber = 0
return answerNumber
def main():
WBSearch = WisdomBagSearch()
workPath = './Plato44'
if not os.path.exists(workPath):
os.mkdir(workPath)
firstLayerUrl = WBSearch.MainUrl + "/category"
categoryHrefS = WBSearch.GetCategory(firstLayerUrl)
secondLayerUrls = WBSearch.preHanle_categoryHrefS(categoryHrefS)
print("--启动YAHOO知惠网网络爬虫--")
for secondLayerUrl in secondLayerUrls:
print(secondLayerUrl)
questionUrls = WBSearch.GetQuestionsUrls(secondLayerUrl)
categoryName = WBSearch.GetCateName(secondLayerUrl)
print('正在爬取类别:' + categoryName)
with open(os.path.join(workPath, categoryName + ".json"), 'w', encoding='utf-8') as w:
s = 1
for questionUrl in questionUrls:
print(questionUrl)
try:
time.sleep(random.random())
questionInformation = WBSearch.GetQuestionMessage(
questionUrl)
print(questionInformation)
if questionInformation == 0:
continue
except Exception as e:
print(type(e), e)
jsonUrl = os.path.join(workPath, categoryName + ".json")
if questionInformation != 0:
if WBSearch.CheckQuestionStandard(questionInformation):
WBSearch.WriteJson(jsonUrl, questionInformation)
def Search(Url):
WBSearch = WisdomBagSearch()
workPath = './Plato43'
secondLayerUrl = Url
categoryName = WBSearch.GetCateName(secondLayerUrl)
print(categoryName)
if not os.path.exists(workPath):
os.mkdir(workPath)
print(secondLayerUrl)
questionUrls = WBSearch.GetQuestionsUrls(secondLayerUrl)
categoryName = WBSearch.GetCateName(secondLayerUrl)
print('正在爬取类别:' + categoryName)
with open(os.path.join(workPath, categoryName + ".json"), 'w', encoding='utf-8') as w:
s = 1
for questionUrl in questionUrls:
print(questionUrl)
try:
time.sleep(random.random())
questionInformation = WBSearch.GetQuestionMessage(questionUrl)
print(questionInformation)
if questionInformation == 0:
continue
except Exception as e:
print(type(e), e)
jsonUrl = os.path.join(workPath, categoryName + ".json")
if questionInformation != 0:
if WBSearch.CheckQuestionStandard(questionInformation):
WBSearch.WriteJson(jsonUrl, questionInformation)
if __name__ == "__main__":
main()
Url = "https://chiebukuro.yahoo.co.jp/category/2078675272/question/list?flg=1"