美丽的汤嵌套div(添加额外的功能)

时间:2014-09-20 23:52:53

标签: python python-2.7 html-parsing beautifulsoup scraper

我正在尝试从[www.quicktransportsolutions.com][1]中提取公司名称,地址和邮政编码。我编写了以下代码来乱写网站并返回我需要的信息。

import requests
from bs4 import BeautifulSoup

def trade_spider(max_pages):
    page = 1
    while page <= max_pages:
        url = 'http://www.quicktransportsolutions.com/carrier/missouri/adrian.php'
        source_code = requests.get(url)
        plain_text = source_code.text
        soup = BeautifulSoup(plain_text)
        for link in soup.findAll('div', {'class': 'well well-sm'}):
            title = link.string
            print(link)
trade_spider(1)

运行代码后,我看到了我想要的信息,但我很困惑如何在没有所有非相关信息的情况下进行打印。

之上
print(link)

我认为我可以使用link.string拉公司名称,但是失败了。有什么建议?

输出:

div class="well well-sm">
<b>2 OLD BOYS TRUCKING LLC</b><br><a href="/truckingcompany/missouri/2-old-boys-trucking-usdot-2474795.php" itemprop="url" target="_blank" title="Missouri Trucking Company 2 OLD BOYS TRUCKING ADRIAN"><u><span itemprop="name"><b>2 OLD BOYS TRUCKING</b></span></u></a><br> <span itemprop="address" itemscope="" itemtype="http://schema.org/PostalAddress"><a href="http://maps.google.com/maps?q=227+E+2ND,ADRIAN,MO+64720&amp;ie=UTF8&amp;z=8&amp;iwloc=addr" target="_blank"><span itemprop="streetAddress">227 E 2ND</span></a>
<br>
<span itemprop="addressLocality">Adrian</span>, <span itemprop="addressRegion">MO</span> <span itemprop="postalCode">64720</span></br></span><br>
                Trucks: 2       Drivers: 2<br>
<abbr class="initialism" title="Unique Number to identify Companies operating commercial vehicles to transport passengers or haul cargo in interstate commerce">USDOT</abbr> 2474795                <br><span class="glyphicon glyphicon-phone"></span><b itemprop="telephone"> 417-955-0651</b>
<br><a href="/inspectionreports/2-old-boys-trucking-usdot-2474795.php" itemprop="url" target="_blank" title="Trucking Company 2 OLD BOYS TRUCKING Inspection Reports">

每个人,

感谢您的帮助到目前为止...我正在尝试为我的小爬虫添加额外的功能。我写了以下代码:

def Crawl_State_Page(max_pages):
    url = 'http://www.quicktransportsolutions.com/carrier/alabama/trucking-companies.php'
    while i <= len(url):
        response = requests.get(url)
        soup = BeautifulSoup(response.content)
        table = soup.find("table", {"class" : "table table-condensed table-striped table-hover table-bordered"})
        for link in table.find_all(href=True):
            print link['href']

Output: 

    abbeville.php
    adamsville.php
    addison.php
    adger.php
    akron.php
    alabaster.php
    alberta.php
    albertville.php
    alexander-city.php
    alexandria.php
    aliceville.php


     alpine.php

... # goes all the way to Z I cut the output short for spacing.. 

我在这里想要完成的是用city.php拉出所有href并将其写入文件。 ..但是现在,我陷入了一个无限循环,它不断循环通过URL。有关如何增加它的任何提示?我的最终目标是创建另一个函数,通过www.site.com/state/city.php反馈到我的trade_spider中,然后循环遍历所有50个日期......有什么效果

while i < len(states,cities):
    url = "http://www.quicktransportsolutions.com/carrier" + states + cities[i] +" 

然后这将循环到我的trade_spider函数中,提取我需要的所有信息。

但是,在我到达那个部分之前,我需要一些帮助摆脱我的无限循环。有什么建议?或者我将要遇到的可预见的问题?

我尝试创建一个循环遍历页面上每个链接的爬虫,然后如果它在页面上找到trade_spider可以抓取的内容,它会将其写入文件...但是,这有点过时了我现在的技能。所以,我正在尝试这种方法。

1 个答案:

答案 0 :(得分:2)

我会依赖每家公司不同标签的itemprop属性。它们可以方便地设置为nameurladdress等:

import requests
from bs4 import BeautifulSoup

def trade_spider(max_pages):
    page = 1
    while page <= max_pages:
        url = 'http://www.quicktransportsolutions.com/carrier/missouri/adrian.php'
        response = requests.get(url)
        soup = BeautifulSoup(response.content)
        for company in soup.find_all('div', {'class': 'well well-sm'}):
            link = company.find('a', itemprop='url').get('href').strip()
            name = company.find('span', itemprop='name').text.strip()
            address = company.find('span', itemprop='address').text.strip()

            print name, link, address
            print "----"

trade_spider(1)

打印:

2 OLD BOYS TRUCKING /truckingcompany/missouri/2-old-boys-trucking-usdot-2474795.php 227 E 2ND

Adrian, MO 64720
----
HILLTOP SERVICE & EQUIPMENT /truckingcompany/missouri/hilltop-service-equipment-usdot-1047604.php ROUTE 2 BOX 453

Adrian, MO 64720
----