0

次の Web サイトから html を解析しています: http://www.asusparts.eu/partfinder/Asus/All In One/E Series たとえば..以下のコードは次を出力します。

datas = s.find(id='accordion')

    a = datas.findAll('a')

    for data in a:

            if(data.has_attr('onclick')):
                model_info.append(data['onclick'])
                print data 

[出力]

<a href="#Bracket" onclick="getProductsBasedOnCategoryID('Asus','Bracket','ET10B','7138', this, 'E Series')">Bracket</a>

これらは私が取得したい値です:

nCategoryID = Bracket

nModelID = ET10B

family = E Series

ページは AJAX からレンダリングされるため、スクリプト ソースを使用しているため、スクリプト ファイルから次の URL が生成されます。

url = 'http://json.zandparts.com/api/category/GetCategories/' + country + '/' + currency + '/' + nModelID + '/' + family + '/' + nCategoryID + '/' + brandName + '/' + null

上記の 3 つの値のみを取得するにはどうすればよいですか?


[編集]


import string, urllib2, urlparse, csv, sys
from urllib import quote
from urlparse import urljoin
from bs4 import BeautifulSoup
from ast import literal_eval

changable_url = 'http://www.asusparts.eu/partfinder/Asus/All%20In%20One/E%20Series'
page = urllib2.urlopen(changable_url)
base_url = 'http://www.asusparts.eu'
soup = BeautifulSoup(page)

#Array to hold all options
redirects = []
#Array to hold all data
model_info = []

print "FETCHING OPTIONS"
select = soup.find(id='myselectListModel')
#print select.get_text()


options = select.findAll('option')

for option in options:
    if(option.has_attr('redirectvalue')):
       redirects.append(option['redirectvalue'])

for r in redirects:
    rpage = urllib2.urlopen(urljoin(base_url, quote(r)))
    s = BeautifulSoup(rpage)
    #print s



    print "FETCHING MAIN TITLE"
    #Finding all the headings for each specific Model
    maintitle = s.find(id='puffBreadCrumbs')
    print maintitle.get_text()

    #Find entire HTML container holding all data, rendered by AJAX
    datas = s.find(id='accordion')

    #Find all 'a' tags inside data container
    a = datas.findAll('a')

    #Find all 'span' tags inside data container
    content = datas.findAll('span')

    print "FETCHING CATEGORY" 

    #Find all 'a' tags which have an attribute of 'onclick' Error:(doesn't display anything, can't seem to find
    #'onclick' attr
    if(hasattr(a, 'onclick')):
        arguments = literal_eval('(' + a['onclick'].replace(', this', '').split('(', 1)[1])
        model_info.append(arguments)
        print arguments #arguments[1] + " " + arguments[3] + " " + arguments[4] 


    print "FETCHING DATA"
    for complete in content:
        #Find all 'class' attributes inside 'span' tags
        if(complete.has_attr('class')):
            model_info.append(complete['class'])

            print complete.get_text()

    #Find all 'table data cells' inside table held in data container       
    print "FETCHING IMAGES"
    img = s.find('td')

    #Find all 'img' tags held inside these 'td' cells and print out
    images = img.findAll('img')
    print images

問題がある場所にエラー行を追加しました...

4

2 に答える 2

1

それを Python リテラル として解析できます。その部分を削除してthis,、括弧内のすべてのもののみを取得すると、次のようになります。

from ast import literal_eval

if data.has_attr('onclick'):
    arguments = literal_eval('(' + data['onclick'].replace(', this', '').split('(', 1)[1])
    model_info.append(arguments)
    print arguments

this引数は有効な python 文字列リテラルではなく、いずれにしても使用したくないため、引数を削除します。

デモ:

>>> literal_eval('(' + "getProductsBasedOnCategoryID('Asus','Bracket','ET10B','7138', this, 'E Series')".replace(', this', '').split('(', 1)[1])
('Asus', 'Bracket', 'ET10B', '7138', 'E Series')

これで、Python タプルが作成され、好きな値を選択できます。

たとえば、インデックス 1、2、および 4 の値が必要です。

nCategoryID, nModelID, family = arguments[1], arguments[3], arguments[4]
于 2013-04-22T12:49:08.817 に答える