How to parse the website using Beautifulsoup

烈酒焚心 提交于 2020-11-28 02:44:29

问题


I am new to web scraping and i want to get the html of the page.But when i run the program i get html empty and console show the javascript

from bs4 import BeautifulSoup
import requests
import urllib

url = "https://linkedin.com/company/1005"

r = requests.get(url)
html_content = r.text
soup = BeautifulSoup(html_content,'html.parser')
print (soup.prettify())


回答1:


Problem is not BeautifulSoup but server which needs more information in requests to give you access to this page. Now it sends JavaScript code which redirects you to login page.

You need User-Agent header to get this page.

You can use http://httpbin.org/get to see User-Agent in your browser.

import requests
from bs4 import BeautifulSoup

headers = {'User-Agent': 'Mozilla/5.0'}

url = "https://linkedin.com/company/1005"

r = requests.get(url, headers=headers)
print(r.text)

soup = BeautifulSoup(r.text, 'html.parser')
print(soup.prettify())


来源:https://stackoverflow.com/questions/40255128/how-to-parse-the-website-using-beautifulsoup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!