Python get all the contents from a website to html file

前端 未结 2 754
攒了一身酷
攒了一身酷 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:12

    because I don't know what site you need scrape so I say a few wasy

    if site contains JS frontend and for laoding needed waiting then I recommend you use requests_html module which has method for rendering content

    from requests_html import HTMLSession
    
    url = "https://some-url.org"
    
    with HTMLSession() as session:
        response = session.get(url)
        response.html.render() #  rendering JS code
        content = response.html.html #  full content
    

    if site doesn't use JS for frontent then requests module is really good choice for you

    import requests
    
    url = "https://some-url.org"
    
    response = requests.get(url)
    content = response.content #  html content in bytes()
    

    else you can use selenium webdriver but it works few slowly for python

提交回复
热议问题