programing tip

뷰티플 스프로 속성 값 추출

itbloger 2020. 9. 22. 07:47
반응형

뷰티플 스프로 속성 값 추출


웹 페이지의 특정 "입력"태그에서 단일 "값"속성의 내용을 추출하려고합니다. 다음 코드를 사용합니다.

import urllib
f = urllib.urlopen("http://58.68.130.147")
s = f.read()
f.close()

from BeautifulSoup import BeautifulStoneSoup
soup = BeautifulStoneSoup(s)

inputTag = soup.findAll(attrs={"name" : "stainfo"})

output = inputTag['value']

print str(output)

TypeError : list indices must be integers, not str

Beautifulsoup 문서에서 나는 문자열이 여기에서 문제가되지 않아야한다는 것을 이해하지만 전문가는 없으며 오해했을 수 있습니다.

어떤 제안이라도 대단히 감사합니다! 미리 감사드립니다.


.findAll() 발견 된 모든 요소의 목록을 반환하므로 :

inputTag = soup.findAll(attrs={"name" : "stainfo"})

inputTag목록 (아마도 하나의 요소 만 포함)입니다. 정확히 원하는 것에 따라 다음 중 하나를 수행해야합니다.

 output = inputTag[0]['value']

또는 .find()하나 (첫 번째) 발견 된 요소 만 반환 하는 메서드를 사용 합니다.

 inputTag = soup.find(attrs={"name": "stainfo"})
 output = inputTag['value']

에서 Python 3.x사용 get(attr_name)하는 태그 객체에 간단히 사용 하십시오 find_all.

xmlData = None

with open('conf//test1.xml', 'r') as xmlFile:
    xmlData = xmlFile.read()

xmlDecoded = xmlData

xmlSoup = BeautifulSoup(xmlData, 'html.parser')

repElemList = xmlSoup.find_all('repeatingelement')

for repElem in repElemList:
    print("Processing repElem...")
    repElemID = repElem.get('id')
    repElemName = repElem.get('name')

    print("Attribute id = %s" % repElemID)
    print("Attribute name = %s" % repElemName)

conf//test1.xml다음과 같은 XML 파일에 대해

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
    <singleElement>
        <subElementX>XYZ</subElementX>
    </singleElement>
    <repeatingElement id="11" name="Joe"/>
    <repeatingElement id="12" name="Mary"/>
</root>

prints:

Processing repElem...
Attribute id = 11
Attribute name = Joe
Processing repElem...
Attribute id = 12
Attribute name = Mary

If you want to retrieve multiple values of attributes from the source above, you can use findAll and a list comprehension to get everything you need:

import urllib
f = urllib.urlopen("http://58.68.130.147")
s = f.read()
f.close()

from BeautifulSoup import BeautifulStoneSoup
soup = BeautifulStoneSoup(s)

inputTags = soup.findAll(attrs={"name" : "stainfo"})
### You may be able to do findAll("input", attrs={"name" : "stainfo"})

output = [x["stainfo"] for x in inputTags]

print output
### This will print a list of the values.

I would actually suggest you a time saving way to go with this assuming that you know what kind of tags have those attributes.

suppose say a tag xyz has that attritube named "staininfo"..

full_tag = soup.findAll("xyz")

And i wan't you to understand that full_tag is a list

for each_tag in full_tag:
    staininfo_attrb_value = each_tag["staininfo"]
    print staininfo_attrb_value

Thus you can get all the attrb values of staininfo for all the tags xyz


you can also use this :

import requests
from bs4 import BeautifulSoup
import csv

url = "http://58.68.130.147/"
r = requests.get(url)
data = r.text

soup = BeautifulSoup(data, "html.parser")
get_details = soup.find_all("input", attrs={"name":"stainfo"})

for val in get_details:
    get_val = val["value"]
    print(get_val)

참고URL : https://stackoverflow.com/questions/2612548/extracting-an-attribute-value-with-beautifulsoup

반응형