做视频网站程序多少钱,二度云自助建站系统,制作网站的步骤和过程,完整的网站开发流程项目中数据分析的需要自己从知乎某个专门的问题上爬数据#xff0c;但众所周知#xff0c;知乎的问题的显示方式有点胃疼#xff08;指滑动后下翻加载更多回答#xff0c;还经常卡住#xff09;#xff0c;翻了翻网上的教程发现有的要么就是很老了要么就是付费的#xf…项目中数据分析的需要自己从知乎某个专门的问题上爬数据但众所周知知乎的问题的显示方式有点胃疼指滑动后下翻加载更多回答还经常卡住翻了翻网上的教程发现有的要么就是很老了要么就是付费的本着开源共赢的原则写一篇记录一下自己踩过的坑也给后面人警醒。
阅读前必知
本文的方法是2023年10月的如果过了时间太久可能就不管用了请注意时效性部分代码由GitHub Copliot完成可能存在错误但是结果应该没问题代码写的比较辣鸡勿喷解决方案也有点繁琐但能用的方法就是好方法
看之前参考了知乎这篇文章
方法1 使用Web scraper
Web scraper是一个很好用的轻量级的0代码爬虫工具只需要安装chrome插件就可以使用在google商店搜就可以了按F12打开是这样的 具体使用过程这里不再赘述记得一定要先选块再选内容。这个的原理和selenium类似模拟滚到顶端然后再收集其实这个用来轻量级爬虫是很好的但对我的任务来说我的任务有2k多条回答很容易滑不到顶端然后出现闪退的情况这里附上我的sitemap对回答较少的问题应该是可以使用的
{_id:name,startUrl:[https://www.zhihu.com/question/xxxxxxxxx/answers/updated],selectors:[{id:block,parentSelectors:[_root],type:SelectorElementScroll,selector:div.List-item:nth-of-type(n2),multiple:true,delay:2000,elementLimit:2100},{id:content,parentSelectors:[block],type:SelectorText,selector:span[itemproptext],multiple:true,regex:},{id:user,parentSelectors:[block],type:SelectorLink,selector:.AuthorInfo-name a,multiple:true,linkType:linkFromHref},{id:date,parentSelectors:[block],type:SelectorText,
selector:.ContentItem-time span,
multiple:true,regex:}]}id就是名字你这个任务的名字然后url里面记得替换你要爬的问题id。
方法2 使用selenium
跟上面的原理差不多滚动到最下面然后抓取页面但跟上面存在相同的滚动满且卡顿、且知乎缓存导致爬不全的问题这里也不多说直接附上代码对小任务应该也是没问题的
def scrape1(question_id):user_agents [Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36]url fhttps://www.zhihu.com/question/{question_id} # 替换question_id# 创建一个Options对象并设置headersoptions Options()options.add_argument(user-agent random.choice(user_agents))# 传入cookiecookies json.load(open(cookie.json, r, encodingutf-8))# options.add_argument(--headless)# 创建WebDriver时传入options参数driver webdriver.Chrome(optionsoptions)driver.get(url)driver.delete_all_cookies()for cookie in cookies:driver.add_cookie(cookie)time.sleep(2)driver.refresh()time.sleep(5) # 等待页面加载完成# items []# question driver.find_element(By.CSS_SELECTOR, div[classQuestionPage] meta[itempropname]).get_attribute(# content)# while True:# # 滚动到页面底部# print(scrolling to bottom)# driver.execute_script(window.scrollTo(0, document.body.scrollHeight);)# time.sleep(random.randint(5, 8)) # 等待页面加载新内容的时间根据实际情况进行调整## # 如果找到了页面底部元素就停止加载# try:# driver.find_element(By.CSS_SELECTOR, button.Button.QuestionAnswers-answerButton)# print(reached the end)# break# except:# pass#html driver.page_source# 解析HTMLsoup BeautifulSoup(html, html.parser)# 获取所有回答的标签answers soup.find_all(div, class_List-item)df pd.DataFrame()contents []answer_ids []driver.quit()for answer in answers:# 获取回答的文本内容content answer.find(div, class_RichContent-inner).get_text()contents.append(content)df[answer_id] answer_idsdf[content] contentsdf.to_csv(f{question_id}.csv, indexFalse, encodingutf-8)这里cookie自己准备要么不好跳过最开始的登录过程。
方法3 使用requests配合beautiful soap
这也是我最后成功的方法最主要的是支持断点接着工作不用拖到底直接使用
这里还参考了这篇文章
https://blog.csdn.net/python03011/article/details/131307051?ops_request_misc%257B%2522request%255Fid%2522%253A%2522169813072516800188539007%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257Drequest_id169813072516800188539007biz_id0utm_mediumdistribute.pc_search_result.none-task-blog-2blogfirst_rank_ecpm_v1~times_rank-3-131307051-null-null.nonecaseutm_term%E7%9F%A5%E4%B9%8E%E9%97%AE%E9%A2%98%E4%B8%8B%E6%89%80%E6%9C%89%E5%9B%9E%E7%AD%94spm1018.2226.3001.4450
原代码的核心代码是这样的
#网址模板
template https://www.zhihu.com/api/v4/questions/432119474/answers?includedata%5B*%5D.is_normal%2Cadmin_closed_comment%2Creward_info%2Cis_collapsed%2Cannotation_action%2Cannotation_detail%2Ccollapse_reason%2Cis_sticky%2Ccollapsed_by%2Csuggest_edit%2Ccomment_count%2Ccan_comment%2Ccontent%2Ceditable_content%2Cattachment%2Cvoteup_count%2Creshipment_settings%2Ccomment_permission%2Ccreated_time%2Cupdated_time%2Creview_info%2Crelevant_info%2Cquestion%2Cexcerpt%2Crelationship.is_authorized%2Cis_author%2Cvoting%2Cis_thanked%2Cis_nothelp%2Cis_labeled%2Cis_recognized%2Cpaid_info%2Cpaid_info_content%3Bdata%5B*%5D.mark_infos%5B*%5D.url%3Bdata%5B*%5D.author.follower_count%2Cbadge%5B*%5D.topics%3Bsettings.table_of_content.enabled%3Boffset{offset}limit5sort_bydefaultplatformdesktopfor page in range(1, 100):#对第page页进行访问url template.format(offsetpage)resp requests.get(url, headersheaders)#解析定位第page页的数据for info in resp.json()[data]:author info[author]Id info[id]text info[excerpt]data {author: author,id: Id,text: text}#存入csvwriter.writerow(data)#降低爬虫对知乎的访问速度time.sleep(1) 但我试了下根本不符合我的要求问题如下
目前知乎改版后excerpt属性并不能得到完整的答案目前知乎不用offset进行翻页了而改用cursorcursor很难找到规律但实际上可以使用每个回答的next的指针。
成功思路
我的思路很简单首先修改上面的代码获取answer_id然后根据answer_id去爬每个对应的完整 回答。
首先说下模版网页如何获取。
我们点开我们想要的回答刷新下找这个包
[外链图片转存中…(img-pkPZH5Pz-1698149682893)]
这个就是我们要用的请求网址可以看到offset一直是0说明不管用了。
解决方法是先用一个起始的url0找到next
import requests
import pandas as pd
import timetemplate https://www.zhihu.com/api/v4/questions/30644408/feeds?cursor1c4cacd45e70f24bd620bad51c605d59includedata[*].is_normal,admin_closed_comment,reward_info,is_collapsed,annotation_action,annotation_detail,collapse_reason,is_sticky,collapsed_by,suggest_edit,comment_count,can_comment,content,editable_content,attachment,voteup_count,reshipment_settings,comment_permission,created_time,updated_time,review_info,relevant_info,question,excerpt,is_labeled,paid_info,paid_info_content,reaction_instruction,relationship.is_authorized,is_author,voting,is_thanked,is_nothelp;data[*].mark_infos[*].url;data[*].author.follower_count,vip_info,badge[*].topics;data[*].settings.table_of_content.enabledlimit5{offset}orderdefaultplatformdesktopsession_id1698132896804376037df pd.DataFrame()
# df有三列answer_id和content以及创建日期
df[answer_id] []
df[content] []
df[created_time] []answer_ids []headers {user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36}cookies {
# 填自己的z_0 cookie}
# 第一条使用模版后面的都是next来获取
url0 template.format(offset0)
resp0 requests.get(url0, headersheaders,cookiescookies)
for data in resp0.json()[data]:answer_id data[target][id]# 添加answer_id到df中answer_ids.append(answer_id)
next resp0.json()[paging][next]for page in range(1,400):# 这里自己估算一下每页是5条数据#对第page页进行访问resp requests.get(next, headersheaders,cookiescookies)print(正在爬取第 str(page) 页)for data in resp.json()[data]:answer_id data[target][id]# 添加answer_id到df中answer_ids.append(answer_id)next resp.json()[paging][next]time.sleep(3) # 这里是情况可快可慢# 将answer_ids写入df
df[answer_id] answer_ids
df.to_csv(answer_id.csv, indexTrue)这样就得到了我们需要的回答的answer_id。
第二步根据answer_id爬内容
from bs4 import BeautifulSoup
import pandas as pd
import randomcontents []batch 0
for answer_id in answer_ids:print(正在爬取answer_id为{answer_id}的数据.format(answer_idanswer_id))url https://www.zhihu.com/question/30644408/answer/{answer_id}.format(answer_idanswer_id)try:resp requests.get(url, headersheaders, cookiescookies)soup BeautifulSoup(resp.text, html.parser)# 查找contentcontent soup.find(div, class_RichContent-inner).textcontents.append(content)print(content)except Exception as e:print(f爬取answer_id为{answer_id}的数据时出现异常{e})breaktime.sleep(random.randint(1,4))# 每爬取100个回答就保存一次数据,保存在不同的文件中if len(contents) % 100 0:new_data {answer_id: answer_ids[:len(contents)], content: contents}new_df pd.DataFrame(new_data)new_df.to_csv(ftext_{batch}.csv, indexTrue)batch 1# new_data {answer_id: answer_ids[:len(contents)], content: contents}
# new_df new_df.append(pd.DataFrame(new_data))
# new_df.to_csv(text1.csv, indexTrue)
这里爬100条保存一次免得前功尽弃。