使用来自网站的抓取链接循环仅打印5个链接

时间:2016-09-22 12:54:38

标签: python-2.7 web-crawler

需要从网址中提到的特定网站打印10个品牌名称,请帮助我

from re import findall
import csv
import re
import urllib
from bs4 import BeautifulSoup

def brand_name_link("https://priceraja.com/mobile/brands/"):    
    htmlfile= urllib.urlopen(url)
    #htmltext = htmlfile.read()
    data=[]
    link=[]
    soup = BeautifulSoup(htmlfile, 'html.parser')
    name_box = soup.find('ul', attrs={'class': 'top-brands'})   

    for tag in name_box.find_all('img'):
        tag.replaceWith('')
        link=tag
    link=name_box.find_all('a')

    print link

1 个答案:

答案 0 :(得分:0)

def brand_name_link(url):   
    htmlfile= urllib.urlopen(url)
    #htmltext = htmlfile.read()
    data=[]
    link=[]
    soup = BeautifulSoup(htmlfile, 'html.parser')
    name_box = soup.find('ul', attrs={'class': 'top-brands'})   

    for tag in name_box.find_all('img'):
        tag.replaceWith('')
        link=tag
    link=name_box.find_all('a')
    k=0
    while k<len(link):
        print link[k]
        k+=1