【发布时间】:2020-08-05 11:26:55
【问题描述】:
我正在尝试从以下位置抓取第二个 HTML 表:'https://www.realclearpolitics.com/epolls/2020/president/pa/pennsylvania_trump_vs_biden-6861.html'
当我运行脚本时,我得到了第一个 Html 表。如何从上面的 URL 中提取第二个表
我尝试使用以下代码:
from bs4 import BeautifulSoup
import requests
import pandas as pd
import re
Res = requests.get('https://www.realclearpolitics.com/epolls/2020/president/pa/pennsylvania_trump_vs_biden-6861.html')
Soup = BeautifulSoup(Res.text , 'html.parser')
table = Soup.find('table',{'class':'data large'})
list_of_rows = []
for row in table.findAll('tr'):
list_of_cells = []
for cell in row.findAll(["td"]):
try:
Cell = cell.find('a', {'class': 'mobile_pollster_name'})
text = re.sub(r'\n\t+', '', Cell.text)
list_of_cells.append(text)
except:
text = re.sub(r'\n\t+', '', cell.text)
list_of_cells.append(text)
pass
list_of_rows.append(list_of_cells)
for item in list_of_rows:
' '.join(item)
data = pd.DataFrame(list_of_rows)
【问题讨论】:
标签: python-3.x beautifulsoup request