Python get all the contents from a website to html file

前端 未结 2 779
攒了一身酷
攒了一身酷 2021-01-28 17:50

someone please help, i want to transfer all to contents from url to a html file can someone help me please? I have to use user-agent too!

2条回答
  •  轮回少年
    2021-01-28 18:21

    Welcome to SO, when you ask a question you need to submit the code that you have tried, here's where you can learn to ask a question properly. Regarding your question, when you say "I want to transfer all to contents from url to a html file" I am assuming you just want to read the page source and save it in a file.

    import requests as r
    from bs4 import BeautifulSoup
    
    data = r.get("http://example.com", headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0')
    soup = BeautifulSoup(data.text)
    
    file = open('myfile.html', 'w')
    file.writelines(soup)
    file.close()
    

    if you get an error called TypeError: write() argument must be str, not Tag, just typecast soup to string.

    file.writelines(str(soup))
    

提交回复
热议问题