使用Python进行网页抓取

时间:2019-12-04 17:18:45

标签: python pandas parsing web-scraping beautifulsoup

如何使用BeautifulSoup从https://ege.hse.ru/rating/2019/81031971/all/?rlist=&ptype=0&vuz-abiturients-budget-order=ge&vuz-abiturients-budget-val=10解析表并制作pandas DataFrame? 我的代码:

import requests
from bs4 import BeautifulSoup
url = 'https://ege.hse.ru/rating/2019/81031971/all/?rlist=&ptype=0&vuz-abiturients-budget-order=ge&vuz-abiturients-budget-val=10'
page = requests.get(url)
soup = BeautifulSoup(page.content,"html.parser")
table = soup.find_all("table")
for each_table in table:
 for row in each_table.find_all('tr'):
  for cell in row.find_all("td"):
   print(cell.text)

我尝试:

import requests
from bs4 import BeautifulSoup
import pandas as pd

url = "https://ege.hse.ru/rating/2019/81031971/all/?rlist=&ptype=0&vuz-abiturients-budget-order=ge&vuz-abiturients-budget-val=10"
page = requests.get(url)

soup = BeautifulSoup(page.content, 'html.parser')

tbl = soup.find("table", {"id": "MainContent_dataGridView1"})

data_frame = pd.read_html(str(tbl))[0]
print(data_frame)

但是它说:

"ValueError: No tables found"

1 个答案:

答案 0 :(得分:0)

我只能看到一个ID为=“ transparence_t”的表

所以:

tbl = soup.find("table", {"id": "transparence_t"})
data_frame = pd.read_html(str(tbl))[0]
print(data_frame)

它向我返回一个698x6数据帧