python:范围未执行

时间:2017-10-05 04:41:09

标签: arrays python-2.7 csv web-scraping beautifulsoup

应用程序执行但范围不执行。在我的CSV文件中,它只显示第一个条目。在刮擦其他字段时,我也遇到了索引超出范围的错误。任何帮助,将不胜感激。我在学。

import requests  
import csv  
from bs4 import BeautifulSoup


f = csv.writer(open('salons.csv', 'w'))  
f.writerow(['Name'])

pages = []


    for i in range(0, 10600):  
url = 'http://www.aveda.com/locator/get_the_facts.tmpl?SalonID=' + str(i) +' '  
    pages.append(url)

for item in pages:  
    page = requests.get(item)  
    soup = BeautifulSoup(page.text, 'lxml')  

salon_name_list = soup.find(class_='getthefacts__store_meta_info--store_phone')  
    salon_name_list_items = salon_name_list.find_all('li', class_='phone')  

for salon_name in salon_name_list_items:  
    names = salon_name.contents[0]

f.writerow([names])

2 个答案:

答案 0 :(得分:1)

您尝试查找电话号码的方式不是您应该做的。电话号码在类名a下的phone标记内。试试这个。它会获取您感兴趣的电话号码:

import requests ; import csv
from bs4 import BeautifulSoup

outfile = open('salons.csv','w')
writer = csv.writer(outfile)
writer.writerow(['Name'])

for i in range(0, 10600):  
    url = 'http://www.aveda.com/locator/get_the_facts.tmpl?SalonID={0}'.format(i)
    page = requests.get(url)
    soup = BeautifulSoup(page.text, 'lxml')
    for salon_name in soup.select('.phone a'):
        names = salon_name.text
        print(names)
        writer.writerow([names])
outfile.close()

答案 1 :(得分:0)

不确定如何缩进代码。在问题中正确格式化。你可能不需要两个for循环。

import requests
import csv
from bs4 import BeautifulSoup

f = csv.writer(open('salons.csv', 'w'))
f.writerow(['Name'])

for i in range(0, 10600):  
    url = 'http://www.aveda.com/locator/get_the_facts.tmpl?SalonID=' + str(i) +'/'
    page = requests.get(url)
    soup = BeautifulSoup(page.text, 'lxml')
    salon_name_list = soup.find(class_='getthefacts__store_meta_info--store_phone')
    salon_name_list_items = salon_name_list.find_all('li', class_='phone')
    for salon_name in salon_name_list_items:
        names = salon_name.contents[0]
        f.writerow([names])