Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.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字符串范围(解析html)_Python_Html_Parsing - Fatal编程技术网

Python字符串范围(解析html)

Python字符串范围(解析html),python,html,parsing,Python,Html,Parsing,在python中,我复制了一个网页,并希望获得在python中解析HTML时出现的所有,我更喜欢。这是假设您想要查找链接,而不仅仅是Python中解析HTML的文本,我更喜欢这样。这是假设您想要查找链接,而不仅仅是文本听起来您需要一个HTML解析器。调查我不会使用正则表达式,它会变得非常混乱,而且容易出错。听起来你需要一个HTML解析器。调查我不会使用正则表达式,它会变得非常混乱,并且容易出错。例如,您可以使用正则表达式来匹配HTML链接或子类Python的内置SGML解析器: from sgm

在python中,我复制了一个网页,并希望获得在python中解析HTML时出现的所有
,我更喜欢。这是假设您想要查找链接,而不仅仅是Python中解析HTML的文本
,我更喜欢这样。这是假设您想要查找链接,而不仅仅是文本
听起来您需要一个HTML解析器。调查我不会使用正则表达式,它会变得非常混乱,而且容易出错。

听起来你需要一个HTML解析器。调查我不会使用正则表达式,它会变得非常混乱,并且容易出错。

例如,您可以使用正则表达式来匹配HTML链接或子类Python的内置SGML解析器:

from sgmllib import SGMLParser

class URLExtractor(SGMLParser):
    def reset(self):
        SGMLParser.reset(self)
        self.urls = []

    def start_a(self, attrs):
        for name, value in attrs:
            if name == 'href':
                self.urls.append(value)
您可以这样使用它:

extractor = URLExtractor()
extractor.feed(html)
print extractor.urls

例如,您可以使用正则表达式来匹配HTML链接或Python内置SGML解析器的子类:

from sgmllib import SGMLParser

class URLExtractor(SGMLParser):
    def reset(self):
        SGMLParser.reset(self)
        self.urls = []

    def start_a(self, attrs):
        for name, value in attrs:
            if name == 'href':
                self.urls.append(value)
您可以这样使用它:

extractor = URLExtractor()
extractor.feed(html)
print extractor.urls
这肯定是一项工作:

>>> from BeautifulSoup import BeautifulSoup
>>> import urllib2
>>> page = urllib2.urlopen('http://stackoverflow.com/')
>>> soup = BeautifulSoup(page)
>>> links = soup.html.body.findAll('a', limit=10)
>>> for i, link in enumerate(links):
...     print i, ':', link.text, ' -- ', link['href'] 
... 
0 : Stack Exchange  --  http://stackexchange.com
1 : log in  --  /users/login
2 : blog  --  http://blog.stackoverflow.com
3 : careers  --  http://careers.stackoverflow.com
4 : chat  --  http://chat.stackoverflow.com
5 : meta  --  http://meta.stackoverflow.com
6 : about  --  /about
7 : faq  --  /faq
8 : Stack Overflow  --  /
9 : Questions  --  /questions
首页上有很多链接;我已将输出限制在前十个

这肯定是一项工作:

>>> from BeautifulSoup import BeautifulSoup
>>> import urllib2
>>> page = urllib2.urlopen('http://stackoverflow.com/')
>>> soup = BeautifulSoup(page)
>>> links = soup.html.body.findAll('a', limit=10)
>>> for i, link in enumerate(links):
...     print i, ':', link.text, ' -- ', link['href'] 
... 
0 : Stack Exchange  --  http://stackexchange.com
1 : log in  --  /users/login
2 : blog  --  http://blog.stackoverflow.com
3 : careers  --  http://careers.stackoverflow.com
4 : chat  --  http://chat.stackoverflow.com
5 : meta  --  http://meta.stackoverflow.com
6 : about  --  /about
7 : faq  --  /faq
8 : Stack Overflow  --  /
9 : Questions  --  /questions

首页上有很多链接;我已将输出限制在前十个

另一个+1表示靓汤。也就是说,如果你真的想要一个非常简单的解析器,你可以使用好的ol正则表达式搜索

>>> import urllib2
>>> response = urllib2.urlopen("http://python.org")
>>> html = response.read()

>>> import re
>>> re.findall("<a[^>]*href=[^>]*>", html)
导入urllib2 >>>响应=urllib2.urlopen(“http://python.org") >>>html=response.read() >>>进口稀土 >>>re.findall(“]*href=[^>]*>”,html)

注意:根据注释更新了regex,使其更加准确,另一个+1表示靓汤。也就是说,如果你真的想要一个非常简单的解析器,你可以使用好的ol正则表达式搜索

>>> import urllib2
>>> response = urllib2.urlopen("http://python.org")
>>> html = response.read()

>>> import re
>>> re.findall("<a[^>]*href=[^>]*>", html)
导入urllib2 >>>响应=urllib2.urlopen(“http://python.org") >>>html=response.read() >>>进口稀土 >>>re.findall(“]*href=[^>]*>”,html)

注意:根据注释更新了regex,使其更加准确,因为没有提到正则表达式:-)Beautiful soup也能够查找电子邮件、电话号码等吗?哦!你想达到什么目的?+1不提正则表达式:-)BeautifulSoup也能找到电子邮件、电话号码等吗?哦!你想达到什么目的?像
这样的链接怎么样?当然-上面的例子主要是第一次通过。我肯定我错过了一些边缘案例。您可能可以使用re.findall(“]*href=[^>]*>”,html)来更准确。再说一次,漂亮的汤可能是一个更好的解决方案。像
这样的链接呢?当然,上面的例子主要是第一次通过。我肯定我错过了一些边缘案例。您可能可以使用re.findall(“]*href=[^>]*>”,html)来更准确。再说一次,漂亮的汤可能是一个更好的解决方案;美丽的群像是非凡的哇;美丽的群像是非凡的