靓汤嵌套div(增加额外功能)

2024-09-27 17:39:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从[www.quicktransportsolutions.com][1]中提取公司名称、地址和zipcode。我写了下面的代码来涂鸦网站并返回我需要的信息。在

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)

运行代码之后,我看到了我想要的信息,但是我不知道如何在没有所有不相关的信息的情况下打印它。在

高于

^{pr2}$

我以为我可以链接.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.. 

我在这里要完成的是使用城市.php把它写进一个文件。。但是现在,我被困在一个无限循环中,它一直在URL中循环。关于如何增加它有什么建议吗?我的最终目标是创建另一个函数,并将其反馈到我的trade\u spider中www.site.com/state/city.php然后循环50次约会。。。大意为

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

然后这将循环到我的trade\u spider函数中,提取我需要的所有信息。在

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

我尝试创建一个爬虫,它会循环浏览页面上的每一个链接,然后如果它在页面上找到trade\u spider可以爬行的内容,它会将其写入一个文件。。。不过,目前来说,这有点超出了我的能力范围。所以,我在尝试这个方法。在


Tags: brcomhttpurlwwwtablelinkclass
1条回答
网友
1楼 · 发布于 2024-09-27 17:39:25

我将依赖于每个公司不同标记的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)

印刷品:

^{pr2}$

相关问题 更多 >

    热门问题