【问题标题】:How to extract table data from a website only AFTER inputting data?如何仅在输入数据后从网站中提取表格数据?
【发布时间】:2020-08-08 06:45:05
【问题描述】:

有一个网站不接受查询(隐藏),有一个带有 html id 的输入字段,一旦你输入值并点击提交,你会得到一个单行表。

是否可以在循环中输入输入值并通过使用 python 和 beautifulsoup 或 flask 进行网络抓取来获取表格数据? (不是硒)

link

点击了解你的班级和部分

`import requests
import urllib.request
import time
from bs4 import BeautifulSoup

# Set the URL you want to webscrape from
url = 'https://www.pesuacademy.com/Academy'
page = requests.get(url)

soup = BeautifulSoup(page.content, 'html.parser')
#results = soup.find(id = "knowClsSectionModalLoginId")
#R = soup.find(id = 'knowClsSectionModalTableDate')
try:
  a = soup.find('input', {'id':'knowClsSectionModalLoginId'}).get('value')
  for i in a:
    inputv = i.get('value')
    print(i, \n)

except:
  pass
`

【问题讨论】:

  • 你能编辑你的问题并在那里发布一些示例输入数据,网站会返回一些输出吗?
  • 完成,但没有结果,我错了
  • 您能否向您的问题提出一些网站返回某些输出的具体值(如 SRN、部门 ID 或手机号码)?
  • 用于 srn 的 PES1201900004

标签: python html web flask beautifulsoup


【解决方案1】:

我假设您指的是“了解您的班级和部门”。这是一种形式。 这是一个带有 loginid 的 ajax 帖子调用。

您可以提供列表loginids 中的所有ID。脚本循环遍历并获取所有数据并保存到 csv 文件中。

import requests
from bs4 import BeautifulSoup
import pandas as pd

loginids = ["PES1201900004"]

payload = {
    "loginId": ""
}

headers = {
    "content-type": "application/x-www-form-urlencoded"
}
url = "https://pesuacademy.com/Academy/getStudentClassInfo"

columns = ['PRN', 'SRN', 'Name', 'Class', 'Section', 'Cycle', 'Department', 'Branch', 'Institute Name']

data = []

for logins in loginids:
    payload["loginId"] = logins

    res = requests.post(url, data=payload,headers=headers)
    soup = BeautifulSoup(res.text, "html.parser")
    data.append([i.get_text(strip=True) for i in soup.find("table").find("tbody").find_all("td")])

df = pd.DataFrame(data, columns=columns)
df.to_csv("data.csv", index=False)
print(df)

输出:

             PRN SRN            Name Class Section Cycle Department  Branch Institute Name
0  PES1201900004  NA  AKSHAYA RAMESH                  NA             B ARCH

【讨论】:

  • 非常感谢,标题是什么?
  • 它们是http请求的组成部分。参考en.wikipedia.org/wiki/List_of_HTTP_header_fields
  • loginids = [] for i in range(0,60): loginids.append("PES120190000"+str(i).zfill(4)) 如果我尝试这个,它会抛出 AttributeError: ' NoneType' 对象没有属性 'find'
  • 使用浏览器检查这些登录 ID 是否可用。如果它们无效,那么您将无法获得正确的 html 响应
猜你喜欢
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 2015-08-26
  • 2020-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多