Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.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(没有第三方解析器)查找所有大写文本的链接?_Python_Html_Parsing - Fatal编程技术网

如何使用Python(没有第三方解析器)查找所有大写文本的链接?

如何使用Python(没有第三方解析器)查找所有大写文本的链接?,python,html,parsing,Python,Html,Parsing,我在一个简单的函数中使用BeautifulSoup来提取所有大写文本的链接: def findAllCapsUrls(page_contents): """ given HTML, returns a list of URLs that have ALL CAPS text """ soup = BeautifulSoup.BeautifulSoup(page_contents) all_urls = node_with_links.findAll(name='a

我在一个简单的函数中使用BeautifulSoup来提取所有大写文本的链接:

def findAllCapsUrls(page_contents):
    """ given HTML, returns a list of URLs that have ALL CAPS text
    """
    soup = BeautifulSoup.BeautifulSoup(page_contents)
    all_urls = node_with_links.findAll(name='a')

    # if the text for the link is ALL CAPS then add the link to good_urls
    good_urls = []
    for url in all_urls:
        text = url.find(text=True)
        if text.upper() == text:
            good_urls.append(url['href'])

    return good_urls
大多数情况下都能正常工作,但由于页面上的HTML格式不正确,少数页面无法在BeautifulSoup(或我也尝试过的lxml)中正确解析,从而导致对象中没有(或只有一些)链接。“一把”可能听起来没什么大不了的,但这个功能正在爬虫程序中使用,因此爬虫程序可能永远找不到数百页

如何将上述函数重构为不使用类似BeautifulSoup的解析器?我已经搜索过如何使用正则表达式来实现这一点,但所有的答案都是“使用BeautifulSoup”。或者,我开始研究如何“修复”格式错误的HTML,以便进行解析,但我认为这不是最好的方法


当我需要解析一个真正破碎的html时,使用re或其他什么替代解决方案可以实现与上述功能相同的功能?

而速度并不是我自动化浏览器的最重要因素

这是我所知道的最难对付的html解析方法。
检查它显示了如何使用webdriver提取google建议(代码是java的,但可以更改为python)。

当我需要解析一个真正破碎的html时,速度并不是我自动化浏览器的最重要因素

这是我所知道的最难对付的html解析方法。
检查它显示了如何使用webdriver提取google建议(代码是java,但可以更改为python)。

如果html页面格式不正确,没有很多解决方案可以真正帮助您。BeautifulSoup或其他解析库是解析html文件的方法


如果您想保留库路径,可以使用regexp来匹配所有链接。请参阅使用[a-Z]

范围。如果html页面格式不正确,没有很多解决方案可以真正帮助您。BeautifulSoup或其他解析库是解析html文件的方法


如果您想保留库路径,可以使用regexp来匹配所有链接。请参见使用[a-Z]范围

def findAllCapsUrls2(page_contents):
    """ returns a list of URLs that have ALL CAPS text, given
    the HTML from a page. Uses a combo of RE and BeautifulSoup
    to handle malformed pages.
    """
    # get all anchors on page using regex
    p = r'<a\s+href\s*=\s*"([^"]*)"[^>]*>(.*?(?=</a>))</a>'
    re_urls = re.compile(p, re.DOTALL)
    all_a = re_urls.findall(page_contents)

    # if the text for the anchor is ALL CAPS then add the link to good_urls
    good_urls = []
    for a in all_a:
        href = a[0]
        a_content = a[1]
        a_soup = BeautifulSoup.BeautifulSoup(a_content)
        text = ''.join([s.strip() for s in a_soup.findAll(text=True) if s])
        if text and text.upper() == text:
            good_urls.append(href)

    return good_urls
def findAllCapsUrls2(第页内容):
“”“返回包含所有大写文本的URL列表,给定
页面中的HTML。使用RE和BeautifulSoup的组合
处理格式错误的页面。
"""
#使用正则表达式获取页面上的所有锚
p=r'))'
re_url=re.compile(p,re.DOTALL)
all_a=re_URL.findall(页面内容)
#如果锚文本都是大写的,则添加指向良好URL的链接
好的URL=[]
对于所有类型的a:
href=a[0]
a_内容=a[1]
a_汤=BeautifulSoup.BeautifulSoup(a_内容)
text=''.join([s.strip(),如果是s,则表示a_soup.findAll(text=True))中的s)
如果text和text.upper()==文本:
好的URL.append(href)
返回正确的URL

到目前为止,这对我的用例是有效的,但我不能保证它对所有页面都有效。此外,我仅在原始函数失败时使用此函数。

我最终使用了regex和BeautifulSoup的组合:

def findAllCapsUrls2(page_contents):
    """ returns a list of URLs that have ALL CAPS text, given
    the HTML from a page. Uses a combo of RE and BeautifulSoup
    to handle malformed pages.
    """
    # get all anchors on page using regex
    p = r'<a\s+href\s*=\s*"([^"]*)"[^>]*>(.*?(?=</a>))</a>'
    re_urls = re.compile(p, re.DOTALL)
    all_a = re_urls.findall(page_contents)

    # if the text for the anchor is ALL CAPS then add the link to good_urls
    good_urls = []
    for a in all_a:
        href = a[0]
        a_content = a[1]
        a_soup = BeautifulSoup.BeautifulSoup(a_content)
        text = ''.join([s.strip() for s in a_soup.findAll(text=True) if s])
        if text and text.upper() == text:
            good_urls.append(href)

    return good_urls
def findAllCapsUrls2(第页内容):
“”“返回包含所有大写文本的URL列表,给定
页面中的HTML。使用RE和BeautifulSoup的组合
处理格式错误的页面。
"""
#使用正则表达式获取页面上的所有锚
p=r'))'
re_url=re.compile(p,re.DOTALL)
all_a=re_URL.findall(页面内容)
#如果锚文本都是大写的,则添加指向良好URL的链接
好的URL=[]
对于所有类型的a:
href=a[0]
a_内容=a[1]
a_汤=BeautifulSoup.BeautifulSoup(a_内容)
text=''.join([s.strip(),如果是s,则表示a_soup.findAll(text=True))中的s)
如果text和text.upper()==文本:
好的URL.append(href)
返回正确的URL

到目前为止,这对我的用例是有效的,但我不能保证它对所有页面都有效。另外,我只在原函数失败时才使用此函数。

我以前遇到过Selenium。。。它看起来比我记得的好。我必须记住那个工具…我以前遇到过Selenium。。。它看起来比我记得的好。我必须记住这个工具……您需要给我们一些BeautifulSoup无法解析的页面示例。如果没有示例可供测试,我们将不知道我们提出的解决方案是否能解决您的问题。您需要给我们一些BeautifulSoup无法解析的页面示例。如果没有可供测试的示例,我们将不知道我们提出的解决方案是否能解决您的问题。