MCPcopy Create free account
hub / github.com/gawel/pyquery / make_links_absolute

Method make_links_absolute

pyquery/pyquery.py:1653–1686  ·  view source on GitHub ↗

Make all links absolute.

(self, base_url=None)

Source from the content-addressed store, hash-verified

1651 return self._parent.base_url
1652
1653 def make_links_absolute(self, base_url=None):
1654 """Make all links absolute.
1655 """
1656 if base_url is None:
1657 base_url = self.base_url
1658 if base_url is None:
1659 raise ValueError((
1660 'You need a base URL to make your links'
1661 'absolute. It can be provided by the base_url parameter.'))
1662
1663 def repl(attr):
1664 def rep(i, e):
1665 attr_value = self(e).attr(attr)
1666 # when label hasn't such attr, pass
1667 if attr_value is None:
1668 return None
1669
1670 # skip specific "protocol" schemas
1671 if any(attr_value.startswith(schema)
1672 for schema in ('tel:', 'callto:', 'sms:')):
1673 return None
1674
1675 return self(e).attr(attr,
1676 urljoin(base_url, attr_value.strip()))
1677 return rep
1678
1679 self('a').each(repl('href'))
1680 self('link').each(repl('href'))
1681 self('script').each(repl('src'))
1682 self('img').each(repl('src'))
1683 self('iframe').each(repl('src'))
1684 self('form').each(repl('action'))
1685
1686 return self
1687
1688
1689build_camel_case_aliases(PyQuery)

Callers 1

test_make_linkMethod · 0.80

Calls 1

eachMethod · 0.80

Tested by 1

test_make_linkMethod · 0.64