【问题标题】:How can I extract the following links from html source code in python?如何从 python 中的 html 源代码中提取以下链接?
【发布时间】:2019-11-12 18:14:05
【问题描述】:

这是我的一些 html 源代码:

<div class="s">
   <div class="th N3nEGc" style="height:48px;width:61px">
<a href="/imgres?imgurl=https://linuxhint.com/wpcontent/uploads/2018/12/11.jpg&amp;imgrefurl=https://linuxhint.com/setup_screensaver_manjaro_linux/&amp;h=912&amp;w=1140&amp;tbnid=10DzCgmImE0jM&amp;tbnh=201&amp;tbnw=251&amp;usg=K_YJsquLr4rorhW2ks8UdceQ8uKjg=&amp;docid=0vImrzSjsr5zQM"
         data-ved="2ahUKEwj3062g3pDjAhWZQN4KHS-_BL8Q8g0wC3oECAUQBQ"
         ping="/urlsa=t&amp;source=web&amp;rct=j&amp;url=/imgres%3Fimgurl%3Dhttps://linuxhint.com/wpcontent/uploads/2018/12/11.jpg%26imgrefurl%3Dhttps://linuxhint.com/setup_screensaver_manjaro_linux/%26h%3D912%26w%3D1140%26tbnid%3D10DzCgmImE0jM%26tbnh%3D201%26tbnw%3D251%26usg%3DK_YJsquLr4rorhW2ks8UdceQ8uKjg%3D%26docid%3D0vImrzSjsr5zQM&amp;ved=2ahUKEwj3062g3pDjAhWZQN4KHS-_BL8Q8g0wC3oECAUQBQ">
      </a>
   </div>
</div>

我要提取的是链接: &lt;a href="/imgres?imgurl=https://linuxhint.com/wpcontent/uploads/2018/12/11.jpg&amp;amp;

所以输出会这样,

https://linuxhint.com/wpcontent/uploads/2018/12/11.jpg

我使用 python 尝试的是:

 sourceCode = opener.open(googlePath).read().decode('utf-8')
 links = re.findall('href="/imgres?imgurl=(.*?)jpg&amp;imgrefurl="',sourceCode)
 for i in links:
    print(i)

【问题讨论】:

    标签: python regex python-3.x web-scraping web-crawler


    【解决方案1】:

    如果问题是你的正则表达式,那么我认为你可以试试这个:

    link = re.search('^https?:\/\/.*[\r\n]*[^.\\,:;]', sourceCode)
    link = link.group()
    print (link)
    

    【讨论】:

      【解决方案2】:

      比通过regex 解析查询字符串更好的方法是使用parse_qs 函数(更安全,您可以在没有regex 摆弄的情况下得到您想要的)(doc):

      data = '''<div class="s"><div class="th N3nEGc" style="height:48px;width:61px"><a href="/imgres?imgurl=https://linuxhint.com/wpcontent/uploads/2018/12/11.jpg&amp;imgrefurl=https://linuxhint.com/setup_screensaver_manjaro_linux/&amp;h=912&amp;w=1140&amp;tbnid=10DzCgmImE0jM&amp;tbnh=201&amp;tbnw=251&amp;usg=K_YJsquLr4rorhW2ks8UdceQ8uKjg=&amp;docid=0vImrzSjsr5zQM" data-ved="2ahUKEwj3062g3pDjAhWZQN4KHS-_BL8Q8g0wC3oECAUQBQ" ping="/urlsa=t&amp;source=web&amp;rct=j&amp;url=/imgres%3Fimgurl%3Dhttps://linuxhint.com/wpcontent/uploads/2018/12/11.jpg%26imgrefurl%3Dhttps://linuxhint.com/setup_screensaver_manjaro_linux/%26h%3D912%26w%3D1140%26tbnid%3D10DzCgmImE0jM%26tbnh%3D201%26tbnw%3D251%26usg%3DK_YJsquLr4rorhW2ks8UdceQ8uKjg%3D%26docid%3D0vImrzSjsr5zQM&amp;ved=2ahUKEwj3062g3pDjAhWZQN4KHS-_BL8Q8g0wC3oECAUQBQ">'''
      
      from bs4 import BeautifulSoup
      from urllib.parse import urlparse, parse_qs
      
      soup = BeautifulSoup(data, 'lxml')
      
      d = urlparse(soup.select_one('a[href*="imgurl"]')['href'])
      q = parse_qs(d.query)
      
      print(q['imgurl'])
      

      打印:

      ['https://linuxhint.com/wpcontent/uploads/2018/12/11.jpg']
      

      【讨论】:

      • 嗨,您的代码在上面的代码中运行良好,但是当我尝试使用整个源代码处理它时,它会显示以下错误。 print(q['imgurl']) KeyError: 'imgurl'
      • @sodmzs 您需要从汤中选择正确的元素。我更新了我的代码。
      • 感谢它现在的工作,但是当源代码中有多个链接在相同的标签内时,它只显示一个链接,就像我问题中的源代码一样。
      • @sodmzs 您需要然后使用 select() 方法,而不是 select_one() 并使用 for-loop 遍历此方法选择的所有链接。
      【解决方案3】:

      也许你应该为“?”添加一个转义字符,试试这个:

      links = re.findall('href="/imgres\?imgurl=(.*?)jpg&amp;imgrefurl="',sourceCode)
      for i in links:
          print(i)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-26
        • 2021-11-03
        • 2013-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多