Guest User

Untitled

a guest
Nov 18th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. def _extract_url_links(html):
  2. """extract url links
  3. >>> _extract_url_links('aa<a href="link1">link1</a>bb<a href="link2">link2</a>cc')
  4. ['link1', 'link2']
  5. """
  6. #"html.parser"はなるべくpython標準のparserモジュールを使うように指定しているBeautifulSoup()で
  7. #BeautifulSoupで扱えるようにしている。
  8. all_url = []
  9. body_soup = BeautifulSoup(html, "html.parser").find('body')
  10. #aタグを全て持ってくる。
  11. for child_tag in body_soup.findChildren():
  12. if child_tag.get('href') is not None:
  13. if '#' not in child_tag.get('href'):#or '.png' or '.jpg' or '.gif'
  14. if '.jpg' not in child_tag.get('href'):
  15. if '.png' not in child_tag.get('href'):
  16. if '.gif' not in child_tag.get('href'):
  17. all_url.append(child_tag.get('href'))
  18. return all_url
  19.  
  20. def _extract_url_links(html):
  21. """extract url links
  22. >>> _extract_url_links('aa<a href="link1">link1</a>bb<a href="link2">link2</a>cc')
  23. ['link1', 'link2']
  24. """
  25. #"html.parser"はなるべくpython標準のparserモジュールを使うように指定しているBeautifulSoup()で
  26. #BeautifulSoupで扱えるようにしている。
  27. all_url = []
  28. body_soup = BeautifulSoup(html, "html.parser").find('body')
  29. #aタグを全て持ってくる。
  30. for child_tag in body_soup.findChildren():
  31. if child_tag.get('href') is not None:
  32. if '#' not in child_tag.get('href') or '.jpg' not in child_tag.get('href') or '.png' not in child_tag.get('href') or'.gif' not in child_tag.get('href'):
  33. return all_url
  34.  
  35. import re
  36. m = re.search(r'ここの引数をどうしたらいいのかわかりません',child_tag.get('href'))
  37. if m is not None;
  38. all_url.append(child_tag.get('href'))
  39.  
  40. #!/usr/bin/python3
  41. # python2ではurlparseというモジュール名でした
  42. from urllib.parse import urlparse
  43.  
  44. # テスト用URL
  45. urls = (
  46. "https://example.com",
  47. "https://example.com/abc.html",
  48. "https://example.com/abc.html#top",
  49. "https://example.com/abc.jpg",
  50. "https://example.com/abc.jpg?v=123",
  51. "https://www.gift.example.com/",
  52. )
  53.  
  54. img_suffixes = (".png", ".jpg", ".gif")
  55. all_url = []
  56. for url in urls:
  57. url_parts = urlparse(url)
  58. if not url_parts.fragment and not url_parts.path.endswith(img_suffixes):
  59. all_url.append(url)
  60. print(all_url)
  61.  
  62. ['https://example.com', 'https://example.com/abc.html', 'https://www.gift.example.com/']
Add Comment
Please, Sign In to add comment