Python 학습 노트(5):상품 데이터 한 페이지 오 르 기

내 코드
from bs4 import BeautifulSoup
import requests
import time


#      
def get_links_from(who_sells):
    links = []
    urls = ['http://bj.58.com/pbdn/{}/pn{}/'.format(who_sells,i) for i in range(1,10)]
    for signal_url in urls:
        wb_data = requests.get(signal_url)
        time.sleep(5)     #        ,      
        if wb_data.status_code == 200:
            soup = BeautifulSoup(wb_data.text, 'lxml')
            for link in soup.select('td.t a.t'):
                links.append(link.get('href'))
    return links

def get_detail(who_sells=0):
    urls = get_links_from(who_sells)
    for url in urls:
        wd_data = requests.get(url)
        soup = BeautifulSoup(wd_data.text, 'lxml')
        
        #                      ,      
        catalogs = soup.select('div > span > a')
        titles = soup.select('.info_titile')
        prices = soup.select('.price_now i')
        tags = soup.select('.biaoqian_li')
        ranges = soup.select('.palce_li span i')
        views = soup.select('.look_time')
        
        #     select           ,          , price[0]
        for catalog, title, price, tag, range,view in zip(catalogs, titles, prices, tags, ranges,views):
            data = {
                'catalog': catalog.get_text(),
                'title': title.get_text(),
                'price': price.get_text(),
                'tag': tag.get_text(),
                'range': range.get_text(),
                'view': view.get_text(),
            }
            print(data)

#            ,0    (  ),1    
get_detail()

총결산
  • select 방법 은 목록 을 되 돌려 줍 니 다
  • 좋은 웹페이지 즐겨찾기