从.aspx页中找到网址的“查询字符串参数”以进行抓取

时间:2019-10-31 14:37:35

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

我正在使用beautifulsouprequests python库进行抓取。通常,可以在浏览器中看到目标页面的URL。但是有时在浏览器中看不到它,因此可以通过Chrome的“开发人员工具”>“网络”标签Query String Parameters轻松学习。

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

有没有人帮助我如何找到此页面上“搜索任何值”的参数?

2 个答案:

答案 0 :(得分:0)

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

您可以使用POST发送requests请求,如下所示:

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

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

您可以在w3schools

阅读更多内容

答案 1 :(得分:0)

它发出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'))
相关问题