【问题标题】:Python Beautiful soup to scrape urls from a web pagePython Beautiful汤从网页中抓取网址
【发布时间】:2017-10-12 08:13:25
【问题描述】:

我正在尝试从 html 格式的网站上抓取网址。我用漂亮的汤。这是html的一部分。

                         <li style="display: block;">
                                <article itemscope itemtype="http://schema.org/Article">
                                    <div class="col-md-3 col-sm-3 col-xs-12" >
                                        <a href="/stroke?p=3083" class="article-image">
                                            <img itemprop="image" src="/FileUploads/Post/3083.jpg?w=300&h=160&mode=crop" alt="Banana" title="Good for health">
                                        </a>
                                    </div>

                                    <div class="col-md-9 col-sm-9 col-xs-12">
                                        <div class="article-content">

                                                <a href="/stroke">
                                                    <img src="/assets/home/v2016/img/icon/stroke.png" style="float:left;margin-right:5px;width: 4%;">
                                                </a>
                                            <a href="/stroke?p=3083" class="article-title">
                                                <div>
                                                    <h4 itemprop="name" id="playground">
Banana Good for health                                                         </h4>
                                                </div>
                                            </a>
                                            <div>                                               
                                                <div class="clear"></div>
                                                <span itemprop="dateCreated" style="font-size:10pt;color:#777;">
                                                    <i class="fa fa-clock-o" aria-hidden="true"></i>
09/10                                                       </span>
                                            </div>
                                            <p itemprop="description" class="hidden-phone">
                                                <a href="/stroke?p=3083">
                                                    I love Banana.
                                                </a>
                                            </p>
                                        </div>
                                    </div>
                                </article>
                            </li>

我的代码:

from bs4 import BeautifulSoup
re=requests.get('http://xxxxxx')
bs=BeautifulSoup(re.text.encode('utf-8'), "html.parser")
for link in bs.find_all('a') :
    if link.has_attr('href'):
        print (link.attrs['href'])

结果将打印出此页面中的所有 url,但这不是我要找的,我只想要一个特定的,比如在这个例子中的 "/stroke?p=3083" 我如何设置条件Python? (我知道这里一共有三个“/stroke?p=3083”,但我只需要一个)

另一个问题。这个网址不完整,我需要将它们与“http://www.abcde.com”结合起来,所以结果将是“http://www.abcde.com/stroke?p=3083”。我知道我可以在 R 中使用 paste,但是如何在 Python 中做到这一点?提前致谢! :)

【问题讨论】:

    标签: python url web-scraping beautifulsoup


    【解决方案1】:

    只需在爬虫中添加一个链接替换 ​​some_link 并试一试。我想你会得到你想要的链接以及它的完整形式。

    import requests
    from bs4 import BeautifulSoup
    from urllib.parse import urljoin
    
    res = requests.get(some_link).text
    soup = BeautifulSoup(res,"lxml")
    for item in soup.select(".article-image"):
        print(urljoin(some_link,item['href']))
    

    【讨论】:

      【解决方案2】:

      另一个问题。这个网址不完整,我需要把它们结合起来 使用“http://www.abcde.com”所以结果将是 “http://www.abcde.com/stroke?p=3083”。我知道我可以在 R 中使用粘贴,但是 如何在 Python 中做到这一点?提前致谢! :)

      link = 'http://abcde.com' + link
      

      【讨论】:

        【解决方案3】:

        您已经掌握了大部分内容。收集如下链接(只是您已经在做的列表理解版本)

        urls = [url for url in bs.findall('a') if url.has_attr('href')]

        这将为您提供网址。要获取其中一个,并将其附加到 abcde url,您只需执行以下操作:

        if urls:
            new_url = 'http://www.abcde.com{}'.format(urls[0])
        

        【讨论】:

        • NoneType object is not callable.
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-28
        • 1970-01-01
        相关资源
        最近更新 更多