I am trying to extract the content of a single "value" attribute in a specific "input" tag on a webpage. I use the following code:
我试图在网页上的特定“输入”标签中提取单个“值”属性的内容。我使用以下代码:
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)
I get a TypeError: list indices must be integers, not str
我得到一个TypeError:列表索引必须是整数,而不是str
even though from the Beautifulsoup documentation i understand that strings should not be a problem here... but i a no specialist and i may have misunderstood.
即使从Beautifulsoup文档我明白字符串不应该是一个问题...但我不是专家,我可能会误解。
Any suggestion is greatly appreciated! Thanks in advance.
任何建议都非常感谢!提前致谢。
5 个解决方案
#1
86
.findAll()
returns list of all found elements, so:
.findAll()返回所有找到的元素的列表,因此:
inputTag = soup.findAll(attrs={"name" : "stainfo"})
inputTag
is a list (probably containing only one element). Depending on what you want exactly you either should do:
inputTag是一个列表(可能只包含一个元素)。根据您的具体需要,您应该做:
output = inputTag[0]['value']
or use .find()
method which returns only one (first) found element:
或者使用.find()方法,它只返回一个(第一个)找到的元素:
inputTag = soup.find(attrs={"name": "stainfo"})
output = inputTag['value']
#2
4
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:
如果要从上面的源中检索多个属性值,可以使用findAll和list comprehension来获取所需的一切:
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.
#3
1
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"..
假设一个标签xyz有那个名为“staininfo”的attritube ..
full_tag = soup.findAll("xyz")
And i wan't you to understand that full_tag is a list
而且你不明白full_tag是一个列表
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
因此,您可以获取所有标签xyz的staininfo的所有attrb值
#4
1
In Python 3.x
, simply use get(attr_name)
on your tag object that you get using find_all
:
在Python 3.x中,只需在使用find_all获取的标记对象上使用get(attr_name):
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)
against XML file conf//test1.xml
that looks like:
针对XML文件conf // test1.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
#5
0
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)
#1
86
.findAll()
returns list of all found elements, so:
.findAll()返回所有找到的元素的列表,因此:
inputTag = soup.findAll(attrs={"name" : "stainfo"})
inputTag
is a list (probably containing only one element). Depending on what you want exactly you either should do:
inputTag是一个列表(可能只包含一个元素)。根据您的具体需要,您应该做:
output = inputTag[0]['value']
or use .find()
method which returns only one (first) found element:
或者使用.find()方法,它只返回一个(第一个)找到的元素:
inputTag = soup.find(attrs={"name": "stainfo"})
output = inputTag['value']
#2
4
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:
如果要从上面的源中检索多个属性值,可以使用findAll和list comprehension来获取所需的一切:
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.
#3
1
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"..
假设一个标签xyz有那个名为“staininfo”的attritube ..
full_tag = soup.findAll("xyz")
And i wan't you to understand that full_tag is a list
而且你不明白full_tag是一个列表
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
因此,您可以获取所有标签xyz的staininfo的所有attrb值
#4
1
In Python 3.x
, simply use get(attr_name)
on your tag object that you get using find_all
:
在Python 3.x中,只需在使用find_all获取的标记对象上使用get(attr_name):
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)
against XML file conf//test1.xml
that looks like:
针对XML文件conf // test1.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
#5
0
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)