报废页面以获取编码的JavaScript链接

时间:2018-10-29 16:39:55

标签: javascript python web-scraping beautifulsoup python-requests

我正在执行一项任务,其中我必须使用python请求库从网页中获取详细信息。我已经从页面成功获取了详细信息,但是它有一个“显示详细信息”按钮,可以使用ajax调用获取更多详细信息,现在我还需要获取那些额外的详细信息。有人可以帮助我吗? 这是网站的链接:-http://ipindiaonline.gov.in/tmrpublicsearch/frmmain.aspx 以及网站的屏幕快照:
enter image description here

这是我编写的代码。

from bs4 import BeautifulSoup
import requests,json

def returnJson(wordmark, page_class):
    url = "http://ipindiaonline.gov.in/tmrpublicsearch/frmmain.aspx"
    r_init = requests.get(url)
    soup = BeautifulSoup(r_init.text, 'html.parser')
    event_validation = soup.find("input", attrs={"name" : "__EVENTVALIDATION"})['value']
    view_state = soup.find("input", attrs={"name" : "__VIEWSTATE"})['value']
    search_type = 'WM'
    postdata = {
    'ctl00$ContentPlaceHolder1$DDLFilter' : '0',
    'ctl00$ContentPlaceHolder1$DDLSearchType' : search_type,
    'ctl00$ContentPlaceHolder1$TBWordmark' : wordmark,
    'ctl00$ContentPlaceHolder1$TBClass' : page_class,
    '__EVENTVALIDATION' : event_validation,
    "__EVENTTARGET" : "ctl00$ContentPlaceHolder1$BtnSearch",
    "__VIEWSTATE" : view_state,
    }

    r = requests.post(url, data=postdata)
    return r

def scrapping(r):
    soup = BeautifulSoup(r.text, 'html.parser')


    counter=len(soup.findAll('tr',attrs={'class':'row'}))
    counter+=len(soup.findAll('tr',attrs={'class':'alt'}))
    wordmark_idvalue='ContentPlaceHolder1_MGVSearchResult_lblsimiliarmark_'
    proprietor_idvalue='ContentPlaceHolder1_MGVSearchResult_LblVProprietorName_'
    applicationno_idvalue='ContentPlaceHolder1_MGVSearchResult_lblapplicationnumber_'
    class_idvalue='ContentPlaceHolder1_MGVSearchResult_lblsearchclass_'
    status_idvalue='ContentPlaceHolder1_MGVSearchResult_Label6_'
    words_list=[]
    for i in range(0,counter):
        words_dict={}
        row=soup.find('span',attrs={'id':(wordmark_idvalue+str(i))})
        words_dict['Wordmark']=row.text
        row=soup.find('span',attrs={'id':(proprietor_idvalue+str(i))})
        words_dict['Proprietor']=row.text
        row=soup.find('span',attrs={'id':(applicationno_idvalue+str(i))})
        words_dict['Application Number']=row.text
        row=soup.find('span',attrs={'id':(class_idvalue+str(i))})
        words_dict['Class ']=row.text
        row=soup.find('span',attrs={'id':(status_idvalue+str(i))})
        words_dict['Status']=row.text
        words_list.append(words_dict)
    return words_list


 def showDetails(wordmark, page_class):
     if(len(wordmark)>2 and page_class.isalnum()==1):
         var=json.dumps(scrapping(returnJson(wordmark, page_class)))
         return var
     else:
        print("Please Enter Valid Parameters\n")

showDetails('AIWA','2')

2 个答案:

答案 0 :(得分:1)

您需要使用第一个POST请求中的信息来创建另一个POST请求。下面显示了如何从返回的数据中提取Goods & Services Description

from operator import itemgetter
from bs4 import BeautifulSoup
import requests,json

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'}

def get_input_value(soup, name):
    return soup.find("input", attrs={"name" : name})['value']


def returnJson(wordmark, page_class):
    url = "http://ipindiaonline.gov.in/tmrpublicsearch/frmmain.aspx"
    r_init = requests.get(url)
    soup = BeautifulSoup(r_init.text, 'html.parser')

    postdata = {
        "ctl00$ContentPlaceHolder1$DDLFilter" : "0",
        "ctl00$ContentPlaceHolder1$DDLSearchType" : "WM",
        "ctl00$ContentPlaceHolder1$TBWordmark" : wordmark,
        "ctl00$ContentPlaceHolder1$TBClass" : page_class,
        "__EVENTVALIDATION" : get_input_value(soup, "__EVENTVALIDATION"),
        "__EVENTTARGET" : "ctl00$ContentPlaceHolder1$BtnSearch",
        "__VIEWSTATE" : get_input_value(soup, "__VIEWSTATE"),
    }

    r = requests.post(url, headers=headers, data=postdata)
    return r


def scrapping(r):
    soup = BeautifulSoup(r.content, 'html.parser')

    counter = len(soup.find_all('tr', attrs={'class':'row'}))
    counter += len(soup.find_all('tr', attrs={'class':'alt'}))

    words_list = []

    fields = [
        ("Wordmark", "ContentPlaceHolder1_MGVSearchResult_lblsimiliarmark_{}"),
        ("Proprietor", "ContentPlaceHolder1_MGVSearchResult_LblVProprietorName_{}"),
        ("Application Number", "ContentPlaceHolder1_MGVSearchResult_lblapplicationnumber_{}"),
        ("Class", "ContentPlaceHolder1_MGVSearchResult_lblsearchclass_{}"),
        ("Status", "ContentPlaceHolder1_MGVSearchResult_Label6_{}"),
    ]

    for index in range(0, counter):
        words_dict = {}
        for key, field in fields:
            words_dict[key] = soup.find('span', attrs={'id' : field.format(index)}).text

        print("Wordmark: {}".format(words_dict["Wordmark"]))

        # Construct a POST request for the Show Details panel

        # Locate matching 'Show details' link
        span = soup.find('span', attrs={'id' : fields[0][1].format(index)})
        a = span.find_next('a', class_='LnkshowDetails')
        lnk_show_details = a['href'].split("'")[1]

        data = {
            "__EVENTTARGET" : lnk_show_details,
            "__VIEWSTATE" : get_input_value(soup, "__VIEWSTATE"),
            "__VIEWSTATEENCRYPTED" : "",
            "__EVENTVALIDATION" : get_input_value(soup, "__EVENTVALIDATION"),
            "__ASYNCPOST" : "true",
        }

        url = "http://ipindiaonline.gov.in/tmrpublicsearch" + soup.form["action"].strip(".")
        r_details = requests.post(url, headers=headers, data=data)
        html = b''.join(itemgetter(7, 8)(r_details.content.split(b"|")))
        soup_details = BeautifulSoup(html, "html.parser")

        details = {}

        for tr in soup_details.find_all('tr'):
            row = [td.text for td in tr.find_all('td')]     # Note: Journal No and Used since would need more work
            details[row[0]] = row[1]

        # Copy description
        desc = 'Goods & Services Description'
        words_dict[desc] = details[desc]

        words_list.append(words_dict)

    return words_list


def showDetails(wordmark, page_class):
    if len(wordmark) > 2 and page_class.isalnum() == 1:
        var = json.dumps(scrapping(returnJson(wordmark, page_class)))
        return var
    else:
        print("Please Enter Valid Parameters\n")

print(showDetails('AIWA','2'))

这将显示:

Wordmark: AIWA
Wordmark: AIWACEM 
Wordmark: AIWAPRIME (LABEL)
[{"Wordmark": "AIWA", "Proprietor": "AIWA CO. LTD.", "Application Number": "683935", "Class": "2", "Status": "Registered", "Goods & Services Description": "PAINTS, VARNISHES, LACQUERS, PRESERVATIVES AGAINST RUST AND AGAINST DESTRIORATION OF WOOD, COLOURING MATTERS, DYESTUFFS, MORDANTS, NATURAL RESINS, METALS IN FOIL AND POWDER FROM FOR PAINTERS AND DECORATORS."}, {"Wordmark": "AIWACEM ", "Proprietor": "AMITA B. MEHTA", "Application Number": "1108415", "Class": "2", "Status": "Registered", "Goods & Services Description": "waterproof cement paint."}, {"Wordmark": "AIWAPRIME (LABEL)", "Proprietor": "AMITA B. MEHTA", "Application Number": "1165809", "Class": "2", "Status": "Registered", "Goods & Services Description": "WATER BASED CEMENT PRIMER INCLUDED IN CLASS 2."}]

注意:返回的数据包含由|字符分隔的其他字段。详细信息的HTML也恰好包含此字符,因此有必要提取字段78以获得HTML。

答案 1 :(得分:0)

我不会使用BeautifulSoup尝试此操作。但是,我认为您可以使用Selenium(请参见 https://selenium-python.readthedocs.io/。)

使用其功能,您可以单击选择的“显示详细信息”按钮,然后等待所请求的信息出现在右侧面板中,然后或多或少地像从该面板中使用BeautifulSoup一样拾取所请求的信息。