马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
各位大佬好,我想要爬取知乎某一个问题下的回答,但是好像被反爬了,不知道怎么解决,以下是我的代码import urllib.request
from lxml import etree
def creat_request():
url = 'https://www.zhihu.com/question/291457090/answer/572425905'
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36'
}
request = urllib.request.Request(url = url,headers = headers)
return request
def get_content(request):
response = urllib.request.urlopen(request)
content = response.read().decode('utf-8')
return content
def down_load(content):
tree = etree.HTML(content)
name_list = tree.xpath('//*[@id="QuestionAnswers-answers"]//div[@class="List-item"]//div[@class="RichContent-inner"]//p//text()')
for i in range(len(name_list)):
name = name_list[i]
print(name)
with open("攀枝花对四川的认同感.txt",'w',encoding='utf-8')as fp:
fp.write(str(name_list))
if __name__ == '__main__':
request = creat_request()
content = get_content(request)
down_load(content)
|