爬虫遇到<noscript>标签怎么解决
<noscript>标签是在浏览器(或者用户浏览标识),没有启动脚本支持的情况下触发的标签,在低级爬虫中,基本都没有配置js引擎,通常这种方式和Ajax异步加载同时使用。用于保护自己不想让爬虫接触的信息。我想爬的网址是https://www.mgtv.com/b/350683/11017269.html?fpa=76&fpos=3&lastp=ch_home
目的是提取出评论,但是被这个noscript标签阻挡了,哪位大神能告诉我怎么去爬{:10_266:}
这个反爬真让人头疼{:10_247:}
import sys
sys.path.append('/home/aistudio/external-libraries')
import re
import requests
from bs4 import BeautifulSoup
def get_div():
url = "https://www.mgtv.com/b/350683/11017269.html?fpa=76&fpos=3&lastp=ch_home"
headers = {
"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:85.0) Gecko/20100101 Firefox/85.0",
"Referer" : "https://www.mgtv.com/b/350683/11017269.html?fpa=76&fpos=3&lastp=ch_home",
"Cookie" : "_source_=C; sessionid=1612414228797; lastActionTime=1612414263957; __STKUUID=4f03c23b-2473-4efd-b053-dca7f1cbb3fa; PLANB_FREQUENCY=YBtXlZNe8SbFKupp_21020410; __random_seed=0.6088150340245597; mba_deviceid=7b8b8a0b-e4a0-a176-696c-5b0cda9c9edd; beta_timer=1612414233215; PM_CHKID=c7d6aa914021ed17; MQGUID=1357149151464439808; __MQGUID=1357149151464439808; muteplaycount=2; muteplaytoday=1; mba_sessionid=eb61226f-3a55-0e25-1633-1a44c6abca9d; mba_last_action_time=1612414233340"
}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'lxml')
divs = soup.find_all("div")
print(divs)
if __name__ == "__main__":
get_div()
[<div data-server-rendered="true" id="__nuxt"><!-- --><div id="__layout"><div><noscript>
请启用 JavaScript
</noscript> <!-- --> <!-- --> <div class="m-video-error-infomessage" style="display:none;"><div class="video-error-infomessage"><h4>您将了解到本次错误原因:</h4><p>错误码:<em></em></p><p>错误详情:<em></em></p><p class="video-error-infomessage-closed"><a href="javascript:;">关闭</a></p></div></div> <!-- --></div></div></div>, <div id="__layout"><div><noscript>
请启用 JavaScript
</noscript> <!-- --> <!-- --> <div class="m-video-error-infomessage" style="display:none;"><div class="video-error-infomessage"><h4>您将了解到本次错误原因:</h4><p>错误码:<em></em></p><p>错误详情:<em></em></p><p class="video-error-infomessage-closed"><a href="javascript:;">关闭</a></p></div></div> <!-- --></div></div>, <div><noscript>
请启用 JavaScript
</noscript> <!-- --> <!-- --> <div class="m-video-error-infomessage" style="display:none;"><div class="video-error-infomessage"><h4>您将了解到本次错误原因:</h4><p>错误码:<em></em></p><p>错误详情:<em></em></p><p class="video-error-infomessage-closed"><a href="javascript:;">关闭</a></p></div></div> <!-- --></div>, <div class="m-video-error-infomessage" style="display:none;"><div class="video-error-infomessage"><h4>您将了解到本次错误原因:</h4><p>错误码:<em></em></p><p>错误详情:<em></em></p><p class="video-error-infomessage-closed"><a href="javascript:;">关闭</a></p></div></div>, <div class="video-error-infomessage"><h4>您将了解到本次错误原因:</h4><p>错误码:<em></em></p><p>错误详情:<em></em></p><p class="video-error-infomessage-closed"><a href="javascript:;">关闭</a></p></div>] 刚试了下,挺简单的,直接上代码import requests
url = 'https://comment.mgtv.com/v4/comment/topComment'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
}
params = {
'subjectType': 'hunantv2014',
'subjectId': '11017269',
'callback': 'jQuery1820687947209701939_1612456297450',
'_support': '10000000',
'_': '1612456299603'
}
res = requests.get(url, params=params, headers=headers)
print(res.text)
过程:
1、打开浏览器右键检查,点Network抓包
2、打开目标URL,等评论加载完成
3、点放大镜(search),搜索第一个评论人的名字---例如:芒果评论君
4、查找结果中查看headers
5、得到Request URL和parameters
6、构建代码,测试,完美拿到response,是一串json数据。。。
7、剩下的自己搞定吧,方法太多了,直接解析json比较快 笨鸟学飞 发表于 2021-2-4 16:30
刚试了下,挺简单的,直接上代码
过程:
我用这个方法去爬了另一个网站(http://xwmyey.xwjy.cn/#/fifth/category/schoolNews/notice)的公告列表。最后返回的是“服务器内部错误”,请问有什么解决办法吗。url = 'http://xwmyey.xwjy.cn/serviceApi/sysNotice/notice/page'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 Edg/90.0.818.62'
}
params = {
'timestamp':'1621240123192'
}
strhtml = requests.get(url,params=params,headers=headers)
print(strhtml.text)
页:
[1]