【问题标题】:find the "Query String Parameters" of an url from .aspx page for scrape从 .aspx 页面中查找 url 的“查询字符串参数”以进行抓取
【发布时间】:2019-10-31 14:37:35
【问题描述】:

我正在使用beautifulsouprequests python 库进行抓取。正常情况下,目标页面的 URL 可以在浏览器上看到。但有时在浏览器中看不到,因此可以通过 Chrome 的开发者工具>网络选项卡Query String Parameters 轻松学习。

但是我找不到https://www.imo-official.org/search.aspx页面的“查询字符串参数”。

有没有人帮我在这个页面上找到“搜索任意值”的参数?

【问题讨论】:

  • 该页面使用搜索参数发布。当您单击“搜索”按钮时,您不会在查询字符串中找到它。
  • 我认为可以通过 URL 中的查询字符串获取结果,例如单击 seacrh 按钮。例如:internationalparceltracking.com/#/search 是我们的目标页面。我们可以通过在 URL 末尾添加 ?barcode=3SABC1234567890&checkIfValid=true&country=CL&language=de 参数来获取搜索结果。所以想学习一下,aspx页面的相似参数是什么?

标签: asp.net python-3.x web-scraping beautifulsoup python-requests


【解决方案1】:

它会发出一个 POST 请求,但您需要先发出请求以获取 cookie 和某些值以供正文发布。参赛者搜索的示例。您可以在网络选项卡中查看此内容。您可能希望通过错误处理来开发以下内容。

import requests
import pandas as pd
from bs4 import BeautifulSoup as bs

data = {
  '__VIEWSTATE': '',
    '__VIEWSTATEGENERATOR': '',
  '__EVENTVALIDATION': '',
  'ctl00$CPH_Main$TextBox1': '',
  'ctl00$CPH_Main$Button1': 'Search',
  'ctl00$CPH_Main$CheckBox_Contestant': 'on',
  'ctl00$CPH_Main$DropDownListFrom': '1959',
  'ctl00$CPH_Main$DropDownListTo': '2019'
}

def get_results(search_term):
    with requests.Session() as s:
        r = s.get('https://www.imo-official.org/search.aspx')
        soup = bs(r.content, 'lxml')
        d = {i['id']:i['value'] for i in soup.select('[type="hidden"]')}
        for k,v in d.items():
            data[k]=v
        data['ctl00$CPH_Main$TextBox1'] = search_term
        r = s.post('https://www.imo-official.org/search.aspx', data=data)
        soup = bs(r.content, 'lxml')
        df = pd.read_html(str(soup.select('table')[1]))[0]
        return df

print(get_results('Zhuo Qun Song'))

【讨论】:

  • 您的代码工作正常,我尝试了request = requests.post(url,files=formdata) 并没有得到正确的响应。所以我明白我需要更多地了解post。那么你有什么建议去理解post的逻辑/规则???
  • aspx 页面期望在这种情况下通过视图状态等和会话 cookie 传递一些验证/添加信息,因此您需要从之前的 GET 中获取这些信息并使用 POST 传递。
【解决方案2】:

您看不到查询字符串,因为在这种情况下,搜索按钮会发送 POST 请求。您会在 GET 请求中看到类似 ?q=cats 的查询字符串。

您可以像这样使用requests 发送POST 请求:

url = "https://example.com"
formdata = {name:'jon',age:'21'}
response = requests.request(method='POST',url=url,data=formdata)

转到网络选项卡,您可以找到表单数据以及您可能希望作为参数传递的其他值。

您可以在w3schools阅读更多内容

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多