Guest User

CSV Links Added to Python

a guest
Dec 10th, 2022
94
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. import os
  2. import pandas as pd
  3. import re
  4. from bs4 import BeautifulSoup
  5. from bs4 import element as bs4_element
  6. from google.colab import drive
  7. import csv
  8. drive.mount("/content/gdrive")
  9.  
  10. os.chdir("/content/gdrive/My Drive/Colab Notebooks/Fix WordPress Links")
  11.  
  12.  
  13. html_doc = """
  14. <!-- wp:paragraph -->
  15. <p>This is a existing link <a class="test" href="https://yahoo.com/">Yahoo</a> Text</p>
  16. <!-- /wp:paragraph -->
  17.  
  18. <!-- wp:paragraph -->
  19. <p>This is another Google Text</p>
  20. <!-- /wp:paragraph -->
  21.  
  22. <!-- wp:paragraph -->
  23. <p>This is another lowercase bing Text</p>
  24. <!-- /wp:paragraph -->
  25.  
  26. <!-- wp:paragraph -->
  27. <p>This is another multi word Active Campaign Text</p>
  28. <!-- /wp:paragraph -->
  29. """
  30.  
  31. soup = BeautifulSoup(html_doc, 'html.parser')
  32.  
  33. # read the CSV file with anchor text and hyperlinks
  34. with open('file.csv', 'r') as csv_file:
  35. reader = csv.reader(csv_file)
  36. hyperlinks = dict(reader)
  37.  
  38. # from bs4 import element as bs4_element
  39. be_navStr = bs4_element.NavigableString
  40.  
  41. hList = [
  42. (anchor_text.strip(), hyperlink.strip()) for
  43. anchor_text, hyperlink in hyperlinks.items()
  44. if anchor_text.strip() and hyperlink.strip() # no blanks
  45. ]
  46.  
  47.  
  48. for txt, link in hList:
  49. navStrs = [
  50. d for d in soup.descendants if type(d) == be_navStr
  51. and f' {} '.format(txt.lower()) in f' {} '.format(d.get_text().strip().lower()) and (' '+txt.lower()+' ') in (' '+d.get_text().strip().lower()+' ')
  52. ]
  53. for ns in navStrs:
  54. tLen, remStr = len(txt), f' {ns.get_text().strip()} '
  55. if remStr[1:-1].lower() == txt.lower():
  56. # to skip if it's already a hyperlink
  57. if ns.parent.name == 'a':
  58. # ns.parent['href'] = link # if you want to replace/update link
  59. continue
  60.  
  61. while f' {txt.lower()} ' in remStr.lower():
  62. sInd = remStr.lower().find(f' {txt.lower()} ') + 1
  63.  
  64. hlTag = soup.new_tag('a', href=link)
  65. hlTag.append(remStr[sInd:sInd + tLen])
  66.  
  67. newCont = [remStr[:sInd].lstrip(), hlTag, ' ']
  68. for addn in newCont: ns.insert_before(addn)
Advertisement
Comments
Add Comment
Please, Sign In to add comment