网页爬取出错
import requestsimport bs4
def open_url(url):
headers = {"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36"}
r=requests.get(url,headers=headers)
return r
def main():
game_url='https://www.bilibili.com/v/game/'
res=open_url(game_url)
with open('bilibiliweb.txt','w') as f:
f.write(res.text)
if __name__=='__main__':
main()
这个编码一直出错,求解{:10_277:}
写入编码改为 utf-8 就好了:
import requests
import bs4
def open_url(url):
headers = {"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36"}
r=requests.get(url,headers=headers)
return r
def main():
game_url='https://www.bilibili.com/v/game/'
res=open_url(game_url)
with open('bilibiliweb.txt','w',encoding='utf-8') as f:
f.write(res.text)
if __name__=='__main__':
main()
页:
[1]