我正在尝试从该站点抓取信息 http://bepi.mpob.gov.my/index.php/en/summary-2/893-summary-2018.html 做一些建模但是 selenium 似乎无法获取表格并通过扩展表格 tr/td 中的任何标签。
这是我的代码
from selenium import webdriver
from pandas import DataFrame
from bs4 import BeautifulSoup
import pandas as pd
import re
options = webdriver.ChromeOptions()
options.add_argument('headless')
path = '/Users/Applications/chromedriver'
driver = webdriver.Chrome(chrome_options= options, executable_path=path)
url = 'http://bepi.mpob.gov.my/index.php/en/summary-2.html'
driver.get(url)
summary_2018 = driver.find_element_by_link_text('Summary 2018')
summary_2018.click()
soup = BeautifulSoup(driver.page_source, 'lxml')
print soup
请您参考如下方法:
您可以使用 requests 获取 iframe URL,然后使用 pandas 获取位于该 url 的表格,从而更快地完成此操作
import pandas as pd
import requests
from bs4 import BeautifulSoup
res = requests.get('http://bepi.mpob.gov.my/index.php/en/summary-2/893-summary-2018.html')
soup = BeautifulSoup(res.content, 'lxml')
iframeURL = soup.select_one('iframe')['src']
results = pd.read_html(iframeURL)
df = results[0].fillna('')
df.iloc[0][0] = 'Category'
print(df)
