我试图在网页上的特定“输入”标签中提取单个“值”属性的内容。我使用以下代码:

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:列表索引必须是整数,而不是str

尽管如此,从Beautifulsoup文档中,我了解到字符串在这里不应该是一个问题……但我不是专家,我可能误解了。

任何建议都非常感谢!


当前回答

下面是一个如何提取所有a标签的href属性的例子:

import requests as rq 
from bs4 import BeautifulSoup as bs

url = "http://www.cde.ca.gov/ds/sp/ai/"
page = rq.get(url)
html = bs(page.text, 'lxml')

hrefs = html.find_all("a")
all_hrefs = []
for href in hrefs:
    # print(href.get("href"))
    links = href.get("href")
    all_hrefs.append(links)

print(all_hrefs)

其他回答

你可以尝试使用名为requests_html的强大的新包:

from requests_html import HTMLSession
session = HTMLSession()

r = session.get("https://www.bbc.co.uk/news/technology-54448223")
date = r.html.find('time', first = True) # finding a "tag" called "time"
print(date)  # you will have: <Element 'time' datetime='2020-10-07T11:41:22.000Z'>
# To get the text inside the "datetime" attribute use:
print(date.attrs['datetime']) # you will get '2020-10-07T11:41:22.000Z'

.find_all()返回所有找到元素的列表,因此:

input_tag = soup.find_all(attrs={"name" : "stainfo"})

Input_tag是一个列表(可能只包含一个元素)。根据你想要什么,你应该做:

output = input_tag[0]['value']

或者使用.find()方法,只返回一个(第一个)找到的元素:

input_tag = soup.find(attrs={"name": "stainfo"})
output = input_tag['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)

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>

打印:

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

你可以试试西班牙凉菜汤:

使用pip Install gazpacho安装它

获取HTML并使用以下方法制作Soup:

from gazpacho import get, Soup

soup = Soup(get("http://ip.add.ress.here/"))  # get directly returns the html

inputs = soup.find('input', attrs={'name': 'stainfo'})  # Find all the input tags

if inputs:
    if type(inputs) is list:
        for input in inputs:
             print(input.attr.get('value'))
    else:
         print(inputs.attr.get('value'))
else:
     print('No <input> tag found with the attribute name="stainfo")

我实际上会建议你一种节省时间的方法,假设你知道什么样的标签有这些属性。

假设标签xyz的属性管名为“staininfo”..

full_tag = soup.findAll("xyz")

我想让你明白full_tag是一个列表

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

因此,您可以获得所有标记xyz的staininfo的所有attrb值