Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用BeautifulSoup解析简短字符串_Python_Beautifulsoup - Fatal编程技术网

Python 使用BeautifulSoup解析简短字符串

Python 使用BeautifulSoup解析简短字符串,python,beautifulsoup,Python,Beautifulsoup,没有办法承认这一点。这个问题出现在这里的唯一原因是一种极端的懒惰,表现为试图避免阅读《圣经》 import urllib2 response = urllib2.urlopen('http://www.crummy.com/software/BeautifulSoup/bs4/doc/') html = response.read() print len(html) 274574字节的BeautifulSoup文档,仅用于处理您在以下几行中看到的相当简单的href标记 mystr = '<

没有办法承认这一点。这个问题出现在这里的唯一原因是一种极端的懒惰,表现为试图避免阅读《圣经》

import urllib2
response = urllib2.urlopen('http://www.crummy.com/software/BeautifulSoup/bs4/doc/')
html = response.read()
print len(html)
274574字节的BeautifulSoup文档,仅用于处理您在以下几行中看到的相当简单的href标记

mystr = '<a href="gazette.html" detail="particularities">Access it!</a>'
soup = BeautifulSoup(mystr, 'html.parser')
print(soup.prettify())
mystr=''
soup=BeautifulSoup(mystr,'html.parser')
打印(soup.prettify())
自然,美化看起来像

<a detail="particularities" href="gazette.html">
 Access it!
</a>

现在我想回答以下问题:

  • mystr
    中确实有
    a
    标记吗?并且,在测试后,有:
  • 有详细标签吗?并且,在测试后,有:
  • 详细信息标记的值是多少
  • 最后,内容是什么?(“访问它!”在本例中)
  • 我读了文件:

    from bs4 import BeautifulSoup
    
    mystr = '<a href="gazette.html" detail="particularities">Access it!</a>'
    soup = BeautifulSoup(mystr, 'html.parser')
    a = soup.findAll('a', detail='particularities')[0]
    print a.text
    
    从bs4导入美化组
    mystr=''
    soup=BeautifulSoup(mystr,'html.parser')
    a=soup.findAll('a',detail='specialities')[0]
    打印文本
    
    • (只是因为我是个好人)

      • 我们可以按顺序回答您的问题

        from bs4 import BeautifulSoup
        
        mystr = '<a href="gazette.html" detail="particularities">Access it!</a>'
        soup = BeautifulSoup(mystr, 'html.parser')
        
        a = soup.find('a')
        if a: # a will be None if there are no <a> tags
            try:
                detail = a['detail']
                print "Detail: {}".format(detail)
                print "Content: {}".format(a.text)
            except KeyError: # will throw KeyError if no key named 'detail'
                print "No details"
                print "Content: {}".format(a.text)
        else:
            print "No <a> tags"
        
        从bs4导入美化组
        mystr=''
        soup=BeautifulSoup(mystr,'html.parser')
        a=汤。查找('a')
        如果a:#如果没有标签,a将为无
        尝试:
        细节=一个['detail']
        打印“详细信息:{}”。格式(详细信息)
        打印“内容:{}”。格式(a.text)
        除了KeyError:#如果没有名为“detail”的键,则将抛出KeyError
        打印“无详细信息”
        打印“内容:{}”。格式(a.text)
        其他:
        打印“无标签”
        
        Ctrl+f组合键确实很神奇:)在BeautifulSoup页面的左侧还有一个不错的导航菜单