私はPythonを初めて使い、最近私のプロジェクト用のウェブサイトからデータを収集するためのWebクローヤを作成しようとしています。データの量はかなり大きいため、収集に時間がかかります。その間、私はいくつかの問題に出会った:Python Web Clawer:クローワをよりうまく動作させる方法
私は
try... expect...
を使用して例外を処理しますが、プログラムは例外によって中断されているようですが、プログラムを中断することなくすべての例外を処理するにはどうすればよいですか?
502悪いゲートウェイ - 私はそれで何ができますか?
本当に助けてくれてありがとう!
後
は私のコードです:(Pythonの2.7、BeautifulSoup4.3.2、XlsxWriter0.8.7)# coding: utf-8
import urllib2
import urllib
import re
from bs4 import BeautifulSoup
import urlparse
import xlsxwriter
import traceback;
def open_with_retries(url):
attempts = 5
for attempt in range(attempts):
try:
return opener.open(url)
except:
if attempt == attempts - 1:
raise
workbook = xlsxwriter.Workbook('Artist_Art_B.xlsx')
worksheet = workbook.add_worksheet()
ro = 0
co = 0
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6')]
# First web
response = open_with_retries(unicode("http://amma.artron.net/artronindex_artist.php"))
content = response.read()
pattern = re.compile(u'<li><a href="artronindex_pic.php(.*?) title="(.*?)".*?</a></li>',re.S)
items = re.findall(pattern,content)
for item in items:
# Second
try:
res2 = open_with_retries(str("http://amma.artron.net/artronindex_pic.php?artist="+item[1]))
soup = BeautifulSoup(res2.read())
tables = soup.find_all('tbody')
if len(tables)>0:
table = tables[0]
rows = table.findChildren('tr')
print item[1]
for row in rows:
links = row.find_all('a',href=True)
for link in links:
url = link['href']
parsed = urlparse.urlparse(url)
sort = urlparse.parse_qs(parsed.query)['sort'][0]
labe = urlparse.parse_qs(parsed.query)['labe'][0]
f = {'sort':sort,'labe':labe}
later = urllib.urlencode(f)
# Third
res3 = open_with_retries(str("http://amma.artron.net/artronindex_auctionseason.php?name="+item[1]+"&"+later))
soup2 = BeautifulSoup(res3.read())
ttables = soup2.findChildren('tbody')
if len(tables)>0:
ttable = ttables[0]
rrows = ttable.findChildren('tr')
for rrow in rrows:
ccells = rrow.findChildren('td')
for ccell in ccells:
vvalue = unicode(ccell.string)
worksheet.write(ro,co,vvalue)
co=co+1
print vvalue
ro = ro+1
co = 0
except Exception:
traceback.print_exc()
workbook.close()
あなたの質問は答えがあまりにも一般的です...良い答えを得るには、壊れた特定のコードを含める必要があります。 – miraculixx
@miraculixxこんにちは、コードを追加しました。お礼を言わせていただきありがとうございます –
このコードは実行されません。より良い質問を書くのに役立つ[このツール](https://github.com/alexmojaki/askso)を試してみてください。 –