Python requests isn't giving me the same HTML as my browser is

前端 未结 6 1000
失恋的感觉
失恋的感觉 2021-01-31 18:56

I am grabbing a Wikia page using Python requests. There\'s a problem, though: the requests request isn\'t giving me the same HTML as my browser is with the very

6条回答
  •  再見小時候
    2021-01-31 19:30

    I suggest that you're not sending the proper header (or sending it wrong) with your request. That's why you are getting different content. Here is an example of a HTTP request with header:

    url = 'https://www.google.co.il/search?q=eminem+twitter'
    user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36'
    
    # header variable
    headers = { 'User-Agent' : user_agent }
    
    # creating request
    req = urllib2.Request(url, None, headers)
    
    # getting html
    html = urllib2.urlopen(req).read()
    

    If you are sure that you are sending right header, but are still getting different html. You can try to use selenium. It will allows you to work with browser directly (or with phantomjs if your machine doesn't have GUI). With selenium you will be able just to grab html directly from browser.

提交回复
热议问题