0
このワークアウトのワークシートをダウンロードしようとしていますが、すべてのワークアウトは別の日に分割されています。完了する必要があるのは、リンクの最後に新しい番号を追加することだけです。ここに私のコードです。解析中にリスト全体を反復する
import urllib
import urllib.request
from bs4 import BeautifulSoup
import re
import os
theurl = "http://www.muscleandfitness.com/workouts/workout-routines/gain-10-pounds-muscle-4-weeks-1?day="
urls = []
count = 1
while count <29:
urls.append(theurl + str(count))
count +=1
print(urls)
for url in urls:
thepage = urllib
thepage = urllib.request.urlopen(urls)
soup = BeautifulSoup(thepage,"html.parser")
init_data = open('/Users/paribaker/Desktop/scrapping/workout/4weekdata.txt', 'a')
workout = []
for data_all in soup.findAll('div',{'class':"b-workout-program-day-exercises"}):
try:
for item in data_all.findAll('div',{'class':"b-workout-part--item"}):
for desc in item.findAll('div', {'class':"b-workout-part--description"}):
workout.append(desc.find('h4',{'class':"b-workout-part--exercise-count"}).text.strip("\n") +",\t")
workout.append(desc.find('strong',{'class':"b-workout-part--promo-title"}).text +",\t")
workout.append(desc.find('span',{'class':"b-workout-part--equipment"}).text +",\t")
for instr in item.findAll('div', {'class':"b-workout-part--instructions"}):
workout.append(instr.find('div',{'class':"b-workout-part--instructions--item workouts-sets"}).text.strip("\n") +",\t")
workout.append(instr.find('div',{'class':"b-workout-part--instructions--item workouts-reps"}).text.strip("\n") +",\t")
workout.append(instr.find('div',{'class':"b-workout-part--instructions--item workouts-rest"}).text.strip("\n"))
workout.append("\n*3")
except AttributeError:
pass
init_data.write("".join(map(lambda x:str(x), workout)))
init_data.close
の問題は、私はそのが正しくリストを反復処理したり、私が必要とサーバーのパーサーをクラッシュしていない文字を追加しないと仮定しているサーバーがタイムアウトということです。 私はまた、すべてのリンクをつかみ、それらをテキスト文書に入れて、このスクリプトのテキストを再度開き、テキストを繰り返して、同じエラーを出した別のスクリプトを書くことも試みました。あなたの考えは?
私は意図的にURLを使用しています - urlsは7行目のtheurl + countを使って作成した新しいリストです。 –
ああ!はい、私はそれを試してみましょう。 –
余分な目に感謝してくれたパーフェクト! –