如何通过bs4传递搜索关键字并获得结果

时间:2015-03-16 10:03:49

标签: python html beautifulsoup html-parsing

def get_main_page_url("https://malwr.com/analysis/search/", strDestPath, strMD5):

    base_url = 'https://malwr.com/'
    url = 'https://malwr.com/account/login/'

    username = 'myname'
    password = 'pswd'

    session = requests.Session()

    # getting csrf value
    response = session.get(url)
    soup = bs4.BeautifulSoup(response.content)
    form = soup.form
    csrf = form.find('input', attrs={'name': 'csrfmiddlewaretoken'}).get('value')
##    csrf1 = form.find('input', attrs ={'name': 'search'}).get('value')

    # logging in
    data = {
        'username': username,
        'password': password,
        'csrfmiddlewaretoken': csrf
    }

    session.post(url, data=data)

    # getting analysis data
    response = session.get(urlparameter)
    soup = bs4.BeautifulSoup(response.content)
    form = soup.form
    csrf = form.find('input', attrs={'name': 'csrfmiddlewaretoken'}).get('value')
##    csrf1 = form.find('input', attrs ={'name': 'search'}).get('value')

    data = {
        'search': strMD5,
        'csrfmiddlewaretoken': csrf
    }

    session.post(urlparameter, data = data)
    response = session.get(urlparameter)
    soup = bs4.BeautifulSoup(response.content)
    print(soup)
    if(None != soup.find('section', id='file').find('table')('tr')[-1].a):
        link = soup.find('section', id='file').find('table')('tr')[-1].a.get('href')
        link = urljoin(base_url, link)

        webFile = session.get(link)
        filename =link.split('/')[-2]
        filename = arg + filename
        localFile = open(filename, 'wb')
        localFile.write(webFile.content)
        webFile.close()
        localFile.close()

我可以通过搜索crftoken来登录。然后我尝试发送MD5来搜索malware.com,但是我无法获取搜索已发送的MD5页面的页面。

我想搜索我们通过crftoken的MD5。 请告诉我代码中的错误。

1 个答案:

答案 0 :(得分:1)

你几乎完成了所有事情。除非您需要将POST请求的结果传递给BeautifulSoup。替换:

session.post(urlparameter, data = data)
response = session.get(urlparameter)

使用:

response = session.post(urlparameter, data=data)

为我工作(我在malwr有一个帐户)。