회사 이름, 주소 및 우편 번호를 [www.quicktransportsolutions.com][1]
에서 추출하려고합니다. 사이트를 긁어 내고 필요한 정보를 반환하기 위해 다음 코드를 작성했습니다.Beautiful Soup nested div (추가 기능 추가)
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&ie=UTF8&z=8&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에 다시 피드 다른 함수를 만드는 것입니다 다음
while i < len(states,cities):
url = "http://www.quicktransportsolutions.com/carrier" + states + cities[i] +"
그리고 효과에 ... 50 일자를 통해 뭔가를 루프 다음이 내 trade_spider 함수에 루프, 필요한 모든 정보를 끌어 오기 것입니다.
하지만 그 부분에 도달하기 전에 무한 루프에서 벗어나기 위해 약간의 도움이 필요합니다. 어떤 제안? 또는 예측할 수있는 문제에 내가 갈 예정입니까?
페이지의 모든 링크를 순환하는 크롤러를 만들려고했는데 trade_spider가 크롤링 할 수있는 페이지의 콘텐츠를 찾으면 파일에 기록합니다. 지금 내 능력 세트에서 벗어났다. 그래서, 나는이 방법을 시도하고있다.