how to pass search key and get result through bs4

限于喜欢 提交于 2019-12-11 12:15:30

问题


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()

I am able to login by searching crftoken. Then I am trying to send MD5 to search on malware.com, however I am not able to get the page that searches the sent MD5 to page.

I want to search the MD5 that we passes through crftoken. Please let me know what is the wrong in code.


回答1:


You've done almost everything correctly. Except that you need to pass the result of the POST request to BeautifulSoup. Replace:

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

with:

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

Worked for me (I had an account at malwr).



来源:https://stackoverflow.com/questions/29074052/how-to-pass-search-key-and-get-result-through-bs4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!