如何用请求和beautifulsoup抓取没有分页的页面?



我正在抓取网页(使用Python请求和beautifulsoup),我需要通过项目列表的所有页面,但我需要单击下一页,代码返回只是第50行在我的代码,直到现在

import pandas as pd
import requests
from bs4 import BeautifulSoup
url = 'http://sistemas.anatel.gov.br/se/public/view/b/licenciamento'
antenas = requests.get(url)
if antenas.status_code == 200:
print('Requisição bem sucedida!')
content = antenas.content
soup = BeautifulSoup(content, 'html.parser')
table = soup.find_all(name='table')
table_str = str(table)
df = pd.read_html(table_str)[0]

我的目标是从所有链接自动刮整个表!

此页面使用单独的AJAX请求http://sistemas.anatel.gov.br/se/public/view/b/lic_table.php来获取表,正如您可以使用浏览器调试工具(F12 ->网络)。对于分页,一个数字似乎与skip表单参数一起传递。

试着把每一页分开,像这样:

url = 'http://sistemas.anatel.gov.br/se/public/view/b/lic_table.php'
result_dfs = []
i = 0
while True:
data = {
'skip': i*50,
'rpp': 50,
'wfid': 'licencas'
}
r = requests.post(url, data=data)
# process the results here...
# df = ...
# break when there are no more results
if len(df.index) == 0:
break
result_dfs.append(df)
i += 1
# put them all together
df = pd.concat(result_dfs)

还有一些其他的表单参数,不确定是否需要。

最新更新